0

I have a test class which I use to check if the connection to the database is established. The credentials are saved in a properties file. When I run the tests in eclipse everything works fine. But when I run a maven build the tests fail because the username used to connect to the database is not the one I set in the properties file. It is the windows username. This is my code:

Properties File:

driverClassName=oracle.jdbc.driver.OracleDriver
user=database_dev1
password=password_dev1
url=jdbc:oracle:thin:@MyAwsomeDatabase:1521:DEV01

Config Class:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories("de.xxx.bvd.mobisl.service")
@PropertySource("classpath:database.properties")
@ComponentScan("de.xxx.bvd.mobisl.service")
public class JPAConfig {

@Value("${driverClassName}")
protected String driverClassName;
@Value("${url}")
protected String url;
@Value("${user}")
protected String username;
@Value("${password}")
protected String password;

private static final Logger logger = Logger.getLogger(JPAConfig.class);

@SuppressWarnings("unchecked")
@Lazy
@Bean
public DataSource dataSource() {
    try {
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        Class<? extends Driver> driver = (Class<? extends Driver>) Class.forName(driverClassName);
        dataSource.setDriverClass(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        logger.info("created DataSource with username " + username + " and password " + password);

        return dataSource;
    } catch (ClassNotFoundException e) {
        logger.error("cannot create datasource!!", e);
        return null;
    }
}

As I said, running from eclipse works fine. The logfile says:

[[XXX-LOG]] 2018-09-04 08:27:23 INFO  JPAConfig:57 - created DataSource with username database_dev1
[[XXX-LOG]] 2018-09-04 08:27:27 INFO  JPAConfigTest:52 - got result from database

But running from maven the logfile says:

[[XXX-LOG]] 2018-09-04 08:27:53 INFO  JPAConfig:57 - created DataSource with username <<Windows-Username>>

How can I tell maven to use the username from the properties file?

Raistlin
  • 997
  • 2
  • 11
  • 33
  • 1
    Do not use propertiy names like `password`. Use `db.password` or something more specific Name. same for user – Jens Sep 04 '18 at 07:12
  • Also use encrypted Passwords: https://stackoverflow.com/questions/37404703/spring-boot-how-to-hide-passwords-in-properties-file – Jens Sep 04 '18 at 07:13
  • I'm eating my keyboard right now! The prefix solved it. Thank you very much. And of course I'll use encrypted passwords ;). – Raistlin Sep 04 '18 at 07:18
  • I am not sure but i think that ${user} is replaced by maven – Jens Sep 04 '18 at 07:22
  • I think so too, yes. I tried it in another project and renamed the property "database-loginname" to "user" and had the same result. Maven used the Windows-Username. So, lesson learned: don't call a property "user" :D – Raistlin Sep 04 '18 at 07:30

1 Answers1

0

${user} is replaced by maven with the environment variable user.

You can get this if you run mvn help:system

Solution rename the property to be more specific like

db.username

A side effect user is very ambiguous in bigger projects. If you rename it it is more cleary where it is used

Jens
  • 67,715
  • 15
  • 98
  • 113