Challenges starting off with Gradle, Spring and DB2

馋奶兔 提交于 2019-12-04 03:53:30

问题


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.

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

My challenges:

  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 ?

  2. 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.

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'
}

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();
    }
}

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;
    }
}

The database.properties file

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

Here is my project structure:

Thanks!


回答1:


  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.

  2. 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', include: '*.jar')



来源:https://stackoverflow.com/questions/26130148/challenges-starting-off-with-gradle-spring-and-db2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!