0

There is application on spring+maven

I want to have different application.properties for different profiles, passed via command line.

my config for database is:

import org.apache.log4j.Logger;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"data"})
@PropertySource("classpath:application.properties")
@ComponentScan(basePackages = {"data.persistence"})
public class PersistenceConfig {
    private static final String PROP_DATABASE_DRIVER = "db.driver";
    private static final String PROP_DATABASE_URL = "db.url";
    private static final String PROP_DATABASE_USERNAME = "db.username";
    private static final String PROP_DATABASE_PASSWORD = "db.password";
    private static final String PROP_HIBERNATE_DIALECT = "db.hibernate.dialect";
    private static final String PROP_HIBERNATE_SHOW_SQL = "db.hibernate.show_sql";
    private static final String PROP_ENTITYMANAGER_PACKAGES_TO_SCAN = "db.entitymanager.packages.to.scan";
    private static final String PROP_HIBERNATE_HBM2DDL_AUTO = "db.hibernate.hbm2ddl.auto";

    @Resource
    private Environment env;
    protected final Logger log = Logger.getLogger(getClass());

   ...
}

If I want to use different profiles for different server(qa and at), I need to create two classes like this and mark is @Profile("qa") and @Profile("at")

Does exist the way to create two application.properties, pass parameter to command line and insert in @(PropertySource) path to qa or at application.properties?

Is the way to insert application.properties file name into @PropertySource before start application?

enter image description here

Roberto
  • 1,288
  • 5
  • 23
  • 47
  • 1
    @PropertySource(value = "${configlocation}") should work? Then you can pass configlocation as a command line arg (https://stackoverflow.com/questions/37052857/spring-overriding-one-application-property-from-command-line) – David Szalai Oct 09 '18 at 12:27
  • 1
    You can rely on profile specific files and let Spring take care of them. Have a look here https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties Basically u define different files application-{profile}.properties and then, when starting the application with one (or more) profile active, those files are loaded. – Raffaele Oct 09 '18 at 12:33

1 Answers1

1

In Spring Boot you can create multiple applications properties (or yaml) files with your profile names:

application-qa.properties
application-at.properties

Each one has properties specific to that environment. Then just pass --spring.profiles.active=qa to your app and only the corresponding properties files will be loaded. These alleviates you from having to maintain multiple Java classes.

Mike
  • 4,722
  • 1
  • 27
  • 40
  • mvn package -Dgroups="data.UnitTest" --spring.profiles.active=at1 returned Unable to parse command line options: Unrecognized option: --spring.profiles.active=at1 – Roberto Oct 09 '18 at 12:41
  • @Raffaele If I launch application on localTomcat, do I need to add this params to maven step? package -Dspring.profiles.active=at1? – Roberto Oct 10 '18 at 10:39
  • It depends. If u launch tomcat via Maven, then u need to add this (but not to the package step). Otherwise if u have a local instance of tomcat u can just put spring.profiles.active inside the $CATALINA_HOME/conf/catalina.properties (avoid -D of course) – Raffaele Oct 10 '18 at 14:17