【发布时间】:2013-03-13 02:41:07
【问题描述】:
我是 Maven 新手,我正在尝试将 Maven 配置为生成 2 个 jar:一个用于开发,一个用于生产。它们之间的唯一区别是 config.properties 文件的数据库连接不同,所以我想我可以使用Maven profiles。
令我惊讶的是我不能同时生成两个文件。使用配置文件时,每次构建时都必须选择配置文件,并且将使用配置文件创建一个 jar(在我的情况下)。问题是它会创建 2 个完全相等的 jar,一个没有分类器,一个有分类器(like myjar.jar 和 myjar-prod.jar)所以如果我想生成 dev 和prod jar 我必须创建 4 个 jars(使用一个配置文件运行第一个 Maven,然后使用另一个配置文件运行)
为什么会这样? 对我来说没有任何意义......但是没关系......
我的问题是:
有没有办法避免生成这两个 jar? 我的意思是,我想拥有不同的配置文件,并且我已经接受(悲痛地)执行多次构建过程(每个配置文件一个),我可以避免每次都有 2 个罐子而只有一个没有分类器吗?
这是我的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.p2p.</groupId>
<artifactId>LoadACHFiles</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyProject</name>
<url>http://maven.apache.org</url>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>config-*.properties</exclude>
</excludes>
</resource>
</resources>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.0</version>
<type>jar</type>
</dependency>
</dependencies>
<profiles>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!--<delete file="${project.build.outputDirectory}/config.properties"/>-->
<copy file="src/main/resources/config-prod.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
【问题讨论】:
-
您使用的是哪个 maven 插件?
-
@vikingsteve 我添加了 pom.xml 以供参考