从Gradle,Spring和DB2开始的挑战 [英] Challenges starting off with Gradle, Spring and DB2

查看:197
本文介绍了从Gradle,Spring和DB2开始的挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对该项目的想法是使用Gradle编写一个简单的Spring纯Java应用程序,该应用程序将连接到DB2数据库并提取一些数据并在控制台上打印。

My idea of the project is to write a simple Spring pure java application using Gradle that will connect to a DB2 database and pull some data and print on the console.

首先,我使用Eclipse Luna创建了Gradle项目。

To start off, I created a Gradle project using Eclipse Luna.

我的挑战:


  1. 我如何在src / main / resources中读取具有db.driver,db.url,db.username和db.password的database.properties文件?

  1. How do I read the database.properties file in src/main/resources which has the db.driver, db.url, db.username and db.password ?

如何告诉Gradle为驱动程序安装db2cc4.jar?我无法使用Gradle依赖项来管理它,因为它是专有的jar。

How do I tell Gradle to pick up my db2cc4.jar for the driver ? I cannot manage it with Gradle dependencies because it is a proprietary jar.

这是我的build.gradle

Here is my build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'

repositories {
    mavenCentral()
 }

jar {
    baseName = 'QueryExecutor'
    version =  '0.1.0'
 }

dependencies {
    compile 'org.springframework:spring-context:4.1.0.RELEASE'
    compile 'org.springframework:spring-jdbc:4.1.0.RELEASE'
    runtime files('lib/db2cc4.jar') 
    testCompile 'junit:junit:4.+'
  }

task wrapper(type: Wrapper) {
    gradleVersion = '2.1'
}

这是我的ApplicationConfig.java

Here is my ApplicationConfig.java

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import org.shrinathk.queryexecutor.persistence.QueryExecutorDAO;

@Configuration
@Import({DatabaseConfig.class})
@PropertySource("classpath:application.properties")
@PropertySource("classpath:database.properties")
public class ApplicationConfig
{
    @Resource
    private Environment env;

    @Bean
    public QueryExecutorDAO queryExecutor()
    {
    return new org.shrinathk.queryexecutor.persistence.QueryExecutorDAO();
    }
}

这是我的DatabaseConfig.java

Here is my DatabaseConfig.java

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class DatabaseConfig
{

    private static final String PROPERTY_NAME_DATABASE_DRIVER   = "db.driver";
    private static final String PROPERTY_NAME_DATABASE_URL      = "db.url";
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";

    @Resource
    private Environment env;

    @Bean
    public DataSource dataSource()
    {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
    dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
    dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
    dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));

    return dataSource;
    }
}

database.properties文件

The database.properties file

db.driver=com.ibm.db2.jcc.DB2Driver
db.url=jdbc:db2://localhost:50000/SAMPLE
db.username=db2admin
db.password=db2admin

这是我的项目结构:

谢谢!

推荐答案


  1. 结构项目的价格(例如 src / main / resources )与maven中的相同。当您准备要部署的工件时,应该将其自动移动到适当的位置。

  1. The structure of the project (e.g. src/main/resources) is the same as it was in maven. When You prepare the artifact to deploy it should be automatically moved to appropriate location.

在添加 db2jcc.ar时依赖项您需要的是 flatDir 或在 dependencies 部分下添加以下代码:

When it comes to adding db2jcc.ar dependency what You need is a flatDir or adding the following piece of code under dependencies section:

compile fileTree(dir:' lib,包括: *。jar)

这篇关于从Gradle,Spring和DB2开始的挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆