2

I'm trying to have my resources copied into a classpath depending on which profile was selected using maven. My resources folder structure is as following:

src/main/resources:
config
production
development
staging

My current not-working config is

<profile>
        <id>development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>envtype</name>
                <value>dev</value>
            </property>
        </activation>

        <build>
            <finalName>Corelay</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.0</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
            </plugins>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.properties</include>
                    </includes>
                    <excludes>
                        <exclude>**/production/**</exclude>
                        <exclude>**/staging/**</exclude>
                    </excludes>
                </resource>
            </resources>
            <testResources>
                <testResource>
                    <directory>src/test/resources</directory>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.properties</include>
                    </includes>
                    <excludes>
                        <exclude>**/production/**</exclude>
                        <exclude>**/staging/**</exclude>
                    </excludes>
                </testResource>
            </testResources>
        </build>
</profile>

In hibernate configuration file under config/hibernate/hibernate-config.xml i request some properties from same package

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:**/jdbc.properties</value>
            <value>classpath*:**/hibernate.properties</value>
        </list>
    </property>
</bean>

but there is an error:

Could not resolve placeholder 'jdbc.driverClassName' in string value "${jdbc.driverClassName}"

this property is defined in that file. What's wrong? And another question is how to make resources copied from those profile folders appear in exactly same output classpath structure? I mean there should be no /production, /development or /staging : just /env

I know I could just put them into separate in but then if there are shared ones (like config in the presented structure) how could I include it too?

user2902211
  • 185
  • 14

1 Answers1

1

Create a folder src/main/config at the same level as src/main/resources. Inside create 3 sub-folders common, dev and production:

|__common

| |__common.properties

|__dev

| |__dev.properties

|__prod

| |__prod.properties

Then configure two profiles, dev and production:

<profiles>
    <profile>
        <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>            
            <build>
              <resources>
                  <resource>
                      <directory>src/main/config/common</directory>
                  </resource>
                   <resource>
                      <directory>src/main/config/dev</directory>
                  </resource>
              </resources>
            </build>
    </profile>
    <profile>
        <id>prod</id>
            <build>
              <resources>
                  <resource>
                      <directory>src/main/config/common</directory>
                  </resource>
                   <resource>
                      <directory>src/main/config/prod</directory>
                  </resource>
              </resources>
            </build>
    </profile>        
</profiles>

With this, mvn clean install copies common.properties and dev.properties to the root of the classpath, as the dev profile is active by default.

mvn clean install -Pprod will then install common.properties and production.properties, but no dev.properties, and also to the root of the classpath.

Angular University
  • 42,341
  • 15
  • 74
  • 81