Maven profiles in pom

老子叫甜甜 提交于 2021-02-11 18:24:40

问题


I am trying to set some system environment variables through maven, based on profiles. So my pom code is

<profile>
    <id>dev</id>
    <activation>
           <property>
              <name>com.xxx.profile</name>
              <value>dev</value>
            </property>
    </activation>
    <properties>                
          <db-url>jdbc:postgresql://abc</db-url>                        
          <db-username>xxx</db-username>
          <db-pwd>yy</db-pwd>           
    </properties>           
  </profile>

So when I build the project, I do mvn clean install -Dcom.xxx.profile=dev

In the code, I have

String urlDB = System.getProperty("db-url");
String username = System.getProperty("db-username");
String password = System.getProperty("db-pwd");

But when I run it, all these variables are null.

System.getProperty("com.xxx.profile") does give the right answer....

Ideas?


回答1:


Maven profiles documentaion states that the activation part contains conditions to trigger the profile, like for your case you need the system property "com.xxx.profile" to have value "dev". Basing on that you can tell that System.getProperty("com.xxx.profile") will return correct because this value is already present in your system (since the profile triggered).

Further to similar question Using the properties tag within maven profiles. Basically the properties' values you define will be used to replace ${propertiesKeys} during the process of filtering the resources. Therefore by using properties you do not define new system properties. Required pom setting (where are the .properties files that you want to parse):

 <build>
     <resources>
         <resource>
              <directory>src/main/resources</directory>
              <filtering>true</filtering>
         </resource>
     </resources>  
 </build>

Properties file example:

database.driverClassName=${database.driverClassName}
database.url=${database.url}

Although then you wouldn't read the system property but the properties file.

properties-maven-plugin

properties-maven-plugin I guess it does exatcly what you want. Checkout the "set-system-properties" at the bottom of usage documentation. You will find code that you can use along with your profiles. Here's an example:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0.0</version>
        <executions>
          <execution>
            <goals>
              <goal>set-system-properties</goal>
            </goals>
            <configuration>
              <properties>
                <property>
                  <name>db-url</name>
                  <value>${db-url}</value>
                </property>
              </properties>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>



回答2:


    <properties>
        <env>dev</env>

    </properties>

    <profiles>

        <profile>
            <id>dev</id>
            <properties>
                <AppName>dev-${project.artifactId}</AppName>

                <build.profile.id>dev</build.profile.id>
                <spring.profiles.active>dev</spring.profiles.active>
                <build.outputDirectory>target</build.outputDirectory>
                <wildfly.plugin.clean>clean</wildfly.plugin.clean>
                <wildfly.plugin.install>install</wildfly.plugin.install>
                <env>dev</env>
            </properties>
        </profile>

        <profile>
            <id>sit</id>
            <properties>
                <AppName>sit-${project.artifactId}</AppName>
                <build.profile.id>sit</build.profile.id>
                <spring.profiles.active>sit</spring.profiles.active>
                <build.outputDirectory>deployment/sit</build.outputDirectory>
                <wildfly.plugin.clean>none</wildfly.plugin.clean>
                <wildfly.plugin.install>none</wildfly.plugin.install>
                <env>sit</env>
            </properties>
        </profile>

        <profile>
            <id>uat</id>
            <properties>
                <AppName>uat-${project.artifactId}</AppName>
                <build.profile.id>uat</build.profile.id>
                <spring.profiles.active>uat</spring.profiles.active>
                <build.outputDirectory>deployment/uat</build.outputDirectory>
                <wildfly.plugin.clean>none</wildfly.plugin.clean>
                <wildfly.plugin.install>none</wildfly.plugin.install>
                <env>uat</env>
            </properties>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <AppName>prod-${project.artifactId}</AppName>
                <build.profile.id>prd</build.profile.id>
                <spring.profiles.active>prd</spring.profiles.active>
                <build.outputDirectory>deployment/prd</build.outputDirectory>
                <wildfly.plugin.clean>none</wildfly.plugin.clean>
                <wildfly.plugin.install>none</wildfly.plugin.install>
                <env>prd</env>
            </properties>
        </profile>
    </profiles>

<plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>**********************${build.profile.id} ** IS RUNNING</echo>
                            </tasks>
                            <tasks>
                                <echo>*********************${build.profile.id} **COPYING
                                    PROPERTIES</echo>
                                <delete file="src/main/resources/application.properties" />
                                <delete file="src/main/resources/log4j2.xml" />

                                <copy
                                    file="src/main/resources/${build.profile.id}/application.properties"
                                    tofile="src/main/resources/application.properties" />
                                <copy file="src/main/resources/${build.profile.id}/log4j2.xml"
                                    tofile="src/main/resources/log4j2.xml" />
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


来源:https://stackoverflow.com/questions/35947096/maven-profiles-in-pom

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