我的 Maven 项目是一个将在 Java 6 和 Java 7 项目中使用的 jar。所以我想有不同的工件可以像这样使用:
<dependency>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0</version>
<classifier>jdk6</classifier>
</dependency>
我还希望它们一次性构建。所以配置文件可能不是要走的路。我尝试使用多个插件执行。
首先,我创建了两个单独的构建目录:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>createClassesDir</id>
<phase>process-resources</phase>
<configuration>
<tasks>
<mkdir dir="${project.build.outputDirectory}/jdk6" />
<mkdir dir="${project.build.outputDirectory}/jdk7" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
然后我尝试配置 maven-compiler-plugin 构建两次,在两个目录中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id>jdk6</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.6</source>
<target>1.6</target>
<fork>true</fork>
<compilerArguments>
<d>${project.build.outputDirectory}/jdk6</d>
</compilerArguments>
</configuration>
</execution>
<execution>
<id>jdk7</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.7</source>
<target>1.7</target>
<fork>true</fork>
<compilerArguments>
<d>${project.build.outputDirectory}/jdk7</d>
</compilerArguments>
</configuration>
</execution>
</executions>
</plugin>
但我无法让它工作。它永远不会编译第二次,因为源文件已经编译:
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ example ---
[INFO] Compiling 1 source file to [...]/example/target/classes
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (jdk6) @ example ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (jdk7) @ example ---
[INFO] Nothing to compile - all classes are up to date
你知道我如何为这两种环境编译吗?