我對該項目的想法是使用Gradle編寫一個簡單的Spring純Java應用程序,該應用程序將連接到DB2數據庫並提取一些數據並在控制檯上打印。以Gradle,Spring和DB2開始的挑戰
首先,我使用Eclipse Luna創建了一個Gradle項目。
我的挑戰:
如何解讀其中有db.driver,db.url配置參數,db.username和db.password的src/main /資源database.properties文件?
我該如何告訴Gradle爲驅動程序提取db2cc4.jar?我無法用Gradle依賴管理它,因爲它是一個專有的jar。
這裏是我的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
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
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文件
db.driver=com.ibm.db2.jcc.DB2Driver
db.url=jdbc:db2://localhost:50000/SAMPLE
db.username=db2admin
db.password=db2admin
這裏是我的項目結構:
謝謝!
謝謝! 'compile fileTree(dir:'lib',include:'* .jar')'工作。 – Shrinath 2014-10-01 08:02:38
還有一個問題 - 屬性文件打包在最終的jar文件中。我如何讓他們分開?屬性文件的整體思想是確保不存在對jar的依賴,並且屬性中的任何更改都不需要jar構建。每當jar執行時,它將從指定的位置(在我們的例子中,該jar在相同的目錄中)拾取屬性。 – Shrinath 2014-10-01 08:08:04
不客氣! – Opal 2014-10-01 08:08:06