maven-compiler-plugin:每次执行更改<outputdirectory>

时间:2017-01-05 11:10:27

标签: maven configuration maven-compiler-plugin

情况

我需要在我的GUI(JavaXF)和我的oracle12c数据库中的JavaStoredProcedures中处理XML文件。

在GUI中我想使用属性绑定。 所以我配置maven-jaxb2-plugin来生成具有属性的源。

另一方面,数据库不支持Java8,因此为 maven-jaxb2-plugin 配置了第二个execution,以便在不同的文件夹中生成没有属性的源两个文件夹,在不同文件夹的相同包中包含相同的Java类。

所以我现在看起来像这样:

 project
 \-target
   \-generated-sources
     +-java7
     | \-package.with.java.files
     |   \-Java7-compilabe-sources*
     \-java8
       \-package.with.java.files
         \-Java8-compilabe-sources*

我接下来要做的是将两个文件夹target/generated-sources/java7target/generated-sources/java8分别编译到的不同执行中的不同输出文件夹target/java7/classestarget/java8/classes行家编译-插件

但是我无法为一次执行更改 maven-compiler-plugin 的输出目录。

这是 maven-compiler-plugin 的当前配置:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <verbose>true</verbose>
      <fork>true</fork>
      <encoding>ISO-8859-1</encoding>
    </configuration>
    <executions>
     <execution>
      <id>java8</id>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <executable>${jdk-1.8}/bin/javac</executable>
        <compilerVersion>1.8</compilerVersion>
        <sourceDirectory>${project.build.directory}/generated-sources/java8</sourceDirectory>
        <outputDirectory>${basedir}/target/java8/classes</outputDirectory>
      </configuration>
     </execution>
    </executions>
  </plugin>

但是从maven输出看来它似乎不尊重<sourceDirectory><outputDirectory>中的任何一个:

[INFO] ------------------------------------------------------------------------
[INFO] Building MyProject 6.0.5
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MyProject ---
[INFO] Deleting /ProjectRoot/target
[INFO]
[INFO] --- maven-jaxb2-plugin:0.13.1:generate (Java8) @ MyProject ---
[INFO] Up-to-date check for source resources [[file:/ProjectRoot/src/main/xsd/parad.xsd, file:/ProjectRoot/src/main/xjb/bindings.xml, file:/ProjectRoot/pom.xml]] and target resources [[]].
[INFO] Sources are not up-to-date, XJC will be executed.
[INFO] Episode file [/ProjectRoot/target\generated-sources\java8\META-INF\sun-jaxb.episode] was augmented with if-exists="true" attributes.
[INFO]
[INFO] --- maven-jaxb2-plugin:0.13.1:generate (Java7) @ MyProject ---
[INFO] Up-to-date check for source resources [[file:/ProjectRoot/src/main/xsd/parad.xsd, file:/ProjectRoot/src/main/xjb/bindings.xml, file:/ProjectRoot/pom.xml]] and target resources [[]].
[INFO] Sources are not up-to-date, XJC will be executed.
[INFO] Episode file [/ProjectRoot/target\generated-sources\java7\META-INF\sun-jaxb.episode] was augmented with if-exists="true" attributes.
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MyProject ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MyProject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 114 source files to /ProjectRoot/target\classes

结果

  • 编译器插件不显示执行ID(如maven-jaxb2-plugin那样)
  • 编译器插件编译target/generated-sources/java7target/generated-sources/java8(每个包含57 * .java文件)虽然在<sourceDirectory>中提供了execution/config
  • 尽管在target/classes
  • 中给出了<outputDirectory>,编译器仍会编译到execution/config

问题

如何强制maven分别将target/generated-sources/java7target/generated-sources/java8中的每一个编译到自己的目标文件夹中?

其他信息

$ mvn --version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13:57:37+02:00)

1 个答案:

答案 0 :(得分:0)

我通过使用 maven-antrun-plugin 解决了这个问题。

首先我通过排除所有文件来完成编译:

    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <verbose>true</verbose>
      <fork>true</fork>
      <encoding>ISO-8859-1</encoding>
      <source>1.8</source>
      <target>1.8</target>
      <executable>${jdk-1.8}/bin/javac</executable>
      <compilerVersion>1.8</compilerVersion>
      <excludes>
        <exclude>**/*.*</exclude>
      </excludes>
    </configuration>

我也转过了 maven-jar-plugin

    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
      <execution>
        <id>default-jar</id>
        <phase>never</phase>
        <configuration>
          <finalName>unwanted</finalName>
          <classifier>unwanted</classifier>
        </configuration>
      </execution>
    </executions>

然后我在 maven-antrun-plugin 中配置了单独的执行来编译eache源文件夹并创建单独的jar。

剩下的是配置 maven-deploy-plugin ,以便将两个jar(及其源jar)正确放置在maven存储库中,以便它们可以被提取为的依赖关系。