pom 中的 Maven 配置文件 [英] Maven profiles in pom

查看:97
本文介绍了pom 中的 Maven 配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据配置文件通过 Maven 设置一些系统环境变量.所以我的 pom 代码是

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>

所以当我构建项目时,我做mvn clean install -Dcom.xxx.profile=dev

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

在代码中,我有

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") 确实给出了正确的答案......

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

想法?

推荐答案

    <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>

这篇关于pom 中的 Maven 配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆