0

I'm trying to get active profile from application.properties file during "maven install". I tried to;

@Value("${spring.profiles.active}")
String activeProfiles;

however, it returns null.

Here is the related part from the application.properties;

#PROFILE SETTINGS
spring.profiles.active=@spring.profiles.active@

Any ideas ?

EDIT:

Here are the classes I'm trouble with in;

The class I'm trying to get active profile;

public class JdoProperties {

    @Value("${spring.profiles.active}")
    String activeProfiles;


    public Properties getProperties() { 
        Properties properties = new Properties();
        try {
            if(activeProfiles.contains("test")) {
                properties.load(new ClassPathResource("application-test.properties").getInputStream());
            } else if(activeProfiles.contains("dev")) {
                properties.load(new ClassPathResource("application-dev.properties").getInputStream());
            } else if(activeProfiles.contains("prod")) {
                properties.load(new ClassPathResource("application-prod.properties").getInputStream());
            }
        } catch(FileNotFoundException e) {
            System.out.println("FileNotFound " + e);
        } catch(IOException e) {
            //TODO: Logger
            System.out.println("IOException " + e);
        }

        return properties;
    }
}

The class I'm trying to use that profile

public final class PMF 
{
    private PMF() {}

    public static PersistenceManagerFactory getPersistenceManagerFactory() {
        JdoProperties jdo_properties = new JdoProperties();
        Properties properties = jdo_properties.getProperties();
        return JDOHelper.getPersistenceManagerFactory(properties);
    }
}

The class that use PMF class;

@Configuration
@Import({ShiroBeanConfiguration.class,
        ShiroAnnotationProcessorConfiguration.class,
        ShiroWebConfiguration.class,
        ShiroWebFilterConfiguration.class})
public class RealmConfig{
...
    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public Realm realm() {
        DataSource dataSourceMySQL = 
                (DataSource) PMF.getPersistenceManagerFactory()
                .getConnectionFactory();
        JdbcRealm jdbcRealm = new JdbcRealm();

        jdbcRealm.setCredentialsMatcher(new PasswordMatcher());
        jdbcRealm.setAuthenticationQuery(AUTHENTICATION_QUERY);
        jdbcRealm.setUserRolesQuery(USER_ROLES_QUERY);
        jdbcRealm.setPermissionsQuery(PERMISSIONS_QUERY);
        jdbcRealm.setDataSource(dataSourceMySQL);
        return jdbcRealm;
    }
...
}

And the error I'm getting;

Caused by: java.lang.NullPointerException
    at com.oe.dr.dream.data.JdoProperties.getProperties(JdoProperties.java:28)
    at com.oe.dr.dream.data.PMF.getPersistenceManagerFactory(PMF.java:14)
    at com.oe.dr.dream.config.RealmConfig.realm(RealmConfig.java:94)
Atilla
  • 33
  • 2
  • 9
  • Sorry, it does not make any change but, thanks @AbhijitBashetti – Atilla Feb 25 '20 at 16:03
  • 1
    1. (except "installation, tests, release,..."), maven has nothing to do with "active spring profile" (it can be set/added at (spring(boot))*runtime* [in various ways with ordered precedence...](https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-adding-active-profiles)) 2. In maven it is "usual" to "[replace](https://code.google.com/archive/p/maven-replacer-plugin/)/[*filter*](https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html)" something into your *.properties, *.xml, ... files. – xerx593 Feb 25 '20 at 16:04
  • 1
    when you go with 2. (activate the `` flag on the (correct) ``(s)), then `mvn install -Dspring.profiles.active=""` should do the job.. (but only relevant for test/release...[it still can be overridden at runtime](https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config)) – xerx593 Feb 25 '20 at 16:09
  • Usually there is no need to do `mvn clean install` in the majority of the cases `mvn clean verify` is sufficient. – khmarbaise Feb 25 '20 at 16:15
  • thanks @xerx593 , I did filtering still it does not work. I thinks there is another issue with the order I guess. In one class I am trying to get the active profile and another class I am trying to use that profile. Is it possible that second class trying to build first before the first class ? If it is, how can I change the order ? – Atilla Feb 26 '20 at 08:15

1 Answers1

0

Looking at your "Edit", (I hope) the solution to your problem should be easy:

  1. In Your RealmConfig, define Realm like:

    @Bean
    @DependsOn("lifecycleBeanPostProcessor") //? I am not sure about that
    public Realm realm(@Autowired datasource) { //! let spring "care for" the datasource
        // use above datasource, return "same as before"...
    }
    
  2. (Assuming profiles in [dev, test, prod]) Introduce (in src/main/resources/) 3 files:

    • application-dev.properties
    • application-test.properties
    • application-prod.properties

    You should have them by now, Spring(!) will "pick them up" according to "active profiles", that's why JdoProperties duplicates spring code/functionality.

  3. Ensure, all of the relevant spring.datasource.* properties (and all profile relevant properties) are in the correspondent application-<profile>.properties file. Common properties to all environments/profiles can be set in application.properties ..resp. application-default.properties(further reading: 'spring default profile`).
  4. If you want different contents of application.properties file for different (spring/maven) profiles, then follow the "filtering approach" (with or without "maven profile" ..the difference is in the propagation of the argument: mvn install -Pdev vs. mvn install -Dspring.profiles.active=dev), if not, please consider to delete the property + placeholder. You can verify the filtering by inspection of target/classes/(!)application.properties. (*)
  5. For (maven&not-maven instructed) tests @ActiveProfiles("test") is strong/has high precedence...
  6. (optionally) Delete PMF and JdoProperties classes.
  7. ...what I else might have forgotten.

(*)Please keep in mind, that application.properties is (only) top 15. "property source" (also for spring.profiles.active).

This Baeldung article offers a good introduction/overview/reference on spring profiles including the integration with maven (Chap. 4.6).

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • Thanks for the answer @xerx593 . My current implementation is really bad. I decided to implement it completely in other way. Thanks for the help – Atilla Feb 26 '20 at 15:27