Java Maven打包-构建环境文件?
我使用maven构建一些java项目并将其编译到JarJava Maven打包-构建环境文件?,java,maven,Java,Maven,我使用maven构建一些java项目并将其编译到Jar 除了这个jar,我还有一些特定于环境的配置文件 是否可以让maven创建多个配置文件 给定其他“环境文件” 配置文件内容: server_url={SERVER_URL} server_port={SERVER_PORT} 环境文件1(用于开发人员) 环境文件2(用于qa) 因此,输出将是: the_main.jar config_dev.ini config_qa.ini 更新 为了澄清,我有多个文件,我只想保留一个foe开发,例如:
除了这个jar,我还有一些特定于环境的配置文件
是否可以让maven创建多个配置文件
给定其他“环境文件” 配置文件内容:
server_url={SERVER_URL}
server_port={SERVER_PORT}
环境文件1(用于开发人员)
环境文件2(用于qa)
因此,输出将是:
the_main.jar
config_dev.ini
config_qa.ini
更新为了澄清,我有多个文件,我只想保留一个foe开发,例如:
- application.xml
- application.db.ini
- application.config.ini
- 应用程序_DEV.xml
- 应用程序QA.xml
- application.db_DEV.ini
- application.db_QA.ini
- application.config_DEV.ini
- application.config_QA.ini
maven antrun插件来实现
首先,您应该声明要写入文件的环境变量
<properties>
...
<server.dev>DEV_SERVER</server.dev>
<port.dev>8080</port.dev>
<server.qa>QA_SERVER</server.qa>
<port.qa>8091</port.qa>
</properties>
我认为您可以结合Maven资源过滤和Maven概要文件特性来实现这一点
pom.xml
<project>
...
<profiles>
<profile>
<id>DEV</id>
<properties>
<config.server>DEV_SERVER</config.server>
<config.port>8080</config.port>
</properties>
</profile>
<profile>
<id>QA</id>
<properties>
<config.server>QA_SERVER</config.server>
<config.port>8091</config.port>
</properties>
</profile>
</profiles>
..
</project>
质量保证
过滤激活
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
</resources>
...
</build>
...
</project>
我希望这会对您有所帮助。这种方法的问题是,整个文件都应该存储在CDATA部分,因为文件很大。我有多个文件,我希望它们不在此文件中。@user648026更新了答案以使用将与这些参数连接在一起的文件。不要这样做,这是一个迟钝的黑客行为,你会后悔的,使用配置文件,就像它们被设计用于此黑客行为一样。@JarrodRoberson是的,我同意99%。但这似乎是在一次构建中生成N个ini文件而不是运行N个概要文件构建的唯一方法,因此我不确定这是否真的是重复的。这是一种更好的方法,但它会覆盖相同的文件,有没有一种方法可以克隆像application_DEV.xml application_QA.xml这样的资源文件?如果您需要在最后保留所有配置文件,我认为您不需要Maven。您只需要手动定义所有配置文件。
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b3</version>
<exclusions>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-nodeps</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>create_file</id>
<phase>compile</phase>
<configuration>
<target>
<taskdef resource="net/sf/antcontrib/antlib.xml"
classpathref="maven.compile.classpath" />
<for list="qa,dev" param="ENV">
<sequential>
<copy file="src/main/resources/config.ini"
tofile="target/config_@{ENV}.ini"/>
<concat destfile="target/config_@{ENV}.ini"
append="true"><![CDATA[
SERVER=${server.@{ENV}}
PORT=${port.@{ENV}}]]>
</concat>
</sequential>
</for>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<project>
...
<profiles>
<profile>
<id>DEV</id>
<properties>
<config.server>DEV_SERVER</config.server>
<config.port>8080</config.port>
</properties>
</profile>
<profile>
<id>QA</id>
<properties>
<config.server>QA_SERVER</config.server>
<config.port>8091</config.port>
</properties>
</profile>
</profiles>
..
</project>
mvn ... -PDEV
MVN ... -PQA
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
</resources>
...
</build>
...
</project>
server_url=${config.server}
server_port=${config.port}