gpt4 book ai didi

java - Maven POM 多环境配置文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:14 24 4
gpt4 key购买 nike

这是<build>来 self 的旧 POM 的标签正在运行:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<wtpversion>2.0</wtpversion>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.6.2</version>
</plugin>
</plugins>
<finalName>test</finalName>
</build>

现在我希望每个阶段都有 3 个不同的 api.properties 文件:prod、dev、test。所以我继续遵循<profile> Maven 教程。这就是我在生产和测试阶段最终得到的结果(完全删除旧的 <build> 标签):

<profiles>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<tasks>
<delete file="${project.build.outputDirectory}/api.prod.properties" />
<delete file="${project.build.outputDirectory}/api.dev.properties" />
<copy file="src/main/resources/api.test.properties"
tofile="${project.build.outputDirectory}/api.properties" />
</tasks>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<wtpversion>2.0</wtpversion>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.6.2</version>
</plugin>
</plugins>
<finalName>test</finalName>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<tasks>
<delete file="${project.build.outputDirectory}/api.test.properties" />
<delete file="${project.build.outputDirectory}/api.dev.properties" />
<copy file="src/main/resources/api.prod.properties"
tofile="${project.build.outputDirectory}/api.properties" />
</tasks>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<wtpversion>2.0</wtpversion>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.6.2</version>
</plugin>
</plugins>
<finalName>test</finalName>
</build>
</profile>
</profiles>

但这并没有真正起作用。将仅生成 1 个 .war,而不是我想要的 3 个 war(test-prod.war、test-dev.war,...),并且生成的 war 将包含所有 3 个 .properties 文件,而不是应有的文件。为什么以及我可以做什么来解决它?我对 Maven 还很陌生...谢谢!

最佳答案

使用

mvn clean compile -P prod,dev,test

此外,最好将构建标签放在配置文件标签之外,并在插件内引用配置文件的属性。例如您可以在个人资料中指定属性的文件夹,然后在其他任何地方引用该属性。

   <profile>
<id>dev</id>
<properties>
<properties.dir>YOUR_DEV_DIR</properties.dir>
</properties>
</profile>

然后您可以在其他任何地方引用${properties.dir}

关于java - Maven POM 多环境配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38244581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com