我有一个简单的 spring mvc 应用程序,使用带有 intellij 的 maven。
您如何为生产和开发创建单独的文件?
例如说我想为 nhibernate 设置生产和开发 mysql 连接字符串?
我怎样才能让它在构建时使用正确的文件来获取配置信息? (以及关于文件命名约定的任何建议?)
最佳答案
为此,使用 ant 任务非常简单。
首先,在 <project>
下创建几个配置文件在你的pom
:
<profiles>
<profile>
<id>build-dev</id>
<activation>
<!-- <activeByDefault>true</activeByDefault> -->
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<config.name>config.dev.properties</config.name>
</properties>
</profile>
<profile>
<id>build-prod</id>
<activation>
<property>
<name>env</name>
<value>prod</value>
</property>
</activation>
<properties>
<config.name>config.prod.properties</config.name>
</properties>
</profile>
</profiles>
然后使用 maven-antrun-plugin
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete file="${project.build.outputDirectory}/config.properties"/>
<copy file="src/main/resources/${config.name}" tofile="${project.build.outputDirectory}/config.properties"/>
<delete file="${project.build.outputDirectory}/config.dev.properties"/>
<delete file="${project.build.outputDirectory}/config.prod.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
现在您只需在运行 mvn
时指定所需的配置文件.如果您想要默认值,请取消注释并放置:
<!-- <activeByDefault>true</activeByDefault> -->
默认情况下您想要的配置文件中的部分。事实上,如果没有指定,构建将在 ant 任务上失败。
关于java - 使用 Maven 在 IntelliJ 中构建本地和生产环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7949253/