maven 多模块项目,编译具有不同 java 版本的模块之一

maven multi module project, compile one of the modules with a different java version

我正在尝试构建一个 Maven 多模块项目,我希望其中两个模块构建在 java 8 上,其中一个模块构建在 java 7

编辑:我也看过 this SO question

所以我尝试了 Maven and Java multi-version modules

提供的解决方案

所以我的父 pom 看起来像

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
    <artifactId>parent</artifactId>
    <name>parent</name>
    <version>1.0.0-SNAPSHOT</version>
    <description>Parent pom </description>
    <packaging>pom</packaging>

    <properties>
        <aspectj.version>1.8.5</aspectj.version>
        <jackson.version>2.4.0</jackson.version>
        <java.version>1.8</java.version>
        <java.source.version>${java.version}</java.source.version>
        <java.target.version>${java.version}</java.target.version>
        <jdk.version>8</jdk.version>
        <jdk>${env.JAVA_HOME_8}</jdk>
        <jersey.version>2.9</jersey.version>
        <groovy.version>2.4.3</groovy.version>
        <slf4j.version>1.7.9</slf4j.version>
        <spock.version>1.0-groovy-2.4</spock.version>
        <spring.boot.version>1.3.5.RELEASE</spring.boot.version>
        <spring.version>4.2.6.RELEASE</spring.version>
        <spring.security.version>3.1.7.RELEASE</spring.security.version>
        <spring.data.version>1.7.0.RELEASE</spring.data.version>
    </properties>

    <dependencyManagement>

    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <executions>
                    <execution>
                        <id>unit-tests</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <phase>test</phase>
                        <configuration>
                            <includes>
                                <include>**/*Test.*</include>
                                <include>**/*Spec.*</include>
                            </includes>
                            <excludes>
                                <exclude>**/*IntegrationTest.*</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/main/groovy</source>
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-test-source</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/test/groovy</source>
                                <source>src/test/java</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <compilerId>groovy-eclipse-compiler</compilerId>
                    <source>${java.source.version}</source>
                    <target>${java.target.version}</target>
                    <showDeprecation>true</showDeprecation>
                    <showWarnings>true</showWarnings>
                    <verbose>true</verbose>
                    <executable>${jdk}/bin/javac</executable>
                    <fork>true</fork>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>2.9.2-01</version>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>2.4.3-01</version>
                    </dependency>
                    <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.11</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                    <outputDirectory>${project.build.directory}/surefire-reports/cobertura</outputDirectory>
                    <instrumentation>
                        <ignoreTrivial>true</ignoreTrivial>
                        <ignores>
                            <ignore>org.slf4j.Logger.*</ignore>
                        </ignores>
                        <excludes>
                            <exclude>**/Application.class</exclude>
                            <exclude>**/ApplicationConfig.class</exclude>
                            <exclude>**/JerseyConfig.class</exclude>
                        </excludes>
                    </instrumentation>
                    <check />
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>cobertura</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <modules>
        <module>model</module>
        <module>persistence</module>
        <module>service</module>
    </modules>
</project>

模块中的 pom 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
    <artifactId>model</artifactId>
    <name>model</name>
    <description>Model forservices</description>
    <packaging>jar</packaging>

    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.example</groupId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <java.version>1.7</java.version>
        <java.source.version>${java.version}</java.source.version>
        <java.target.version>${java.version}</java.target.version>
        <jdk.version>7</jdk.version>
        <jdk>${env.JAVA_HOME_7}</jdk>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.raml.plugins</groupId>
                <artifactId>raml-jaxrs-maven-plugin</artifactId>
                <version>1.3.3</version>
                <configuration>
                    <jaxrsVersion>2.0</jaxrsVersion>
                    <jsonMapper>jackson2</jsonMapper>
                    <jsonMapperConfiguration>
                        <generateBuilders>true</generateBuilders>
                        <includeHashcodeAndEquals>true</includeHashcodeAndEquals>
                        <includeToStringuseLongIntegers>true</includeToStringuseLongIntegers>
                    </jsonMapperConfiguration>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-model</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourcePaths>
                                <sourcePath>${basedir}/src/main/raml/services.raml</sourcePath>
                            </sourcePaths>
                            <basePackageName>com.example.app</basePackageName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

但是在编译时出现以下错误:

[INFO] Found location </Users/someuser/.m2/repository/org/codehaus/groovy/groovy-eclipse-batch/2.4.3-01/groovy-eclipse-batch-2.4.3-01.jar> for className <org.eclipse.jdt.internal.compiler.batch.Main>
[INFO] no javaAgentClass seems to be set
[INFO] Compiling in a forked process using /Users/someuser/.m2/repository/org/codehaus/groovy/groovy-eclipse-batch/2.4.3-01/groovy-eclipse-batch-2.4.3-01.jar
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Failure executing groovy-eclipse compiler:
javac: invalid flag: -jar
Usage: javac <options> <source files>
use -help for a list of possible options

[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.687s
[INFO] Finished at: Thu May 26 10:04:15 EDT 2016
[INFO] Final Memory: 21M/320M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project model: Compilation failure
[ERROR] Failure executing groovy-eclipse compiler:
[ERROR] javac: invalid flag: -jar
[ERROR] Usage: javac <options> <source files>
[ERROR] use -help for a list of possible options

编辑::

所以我尝试了基于 A. Di Matteo 的回答

  1. 从 maven-compiler-plugin 的配置部分删除 fork 和可执行选项
  2. 在需要不同版本 java
  3. 的模块上将源和目标版本设置为 1.7

构建成功。但是当我阅读构建清单时,它说 Build-Jdk: 1.8.0_40。那是什么意思

Manifest-Version: 1.0
Implementation-Title: model
Build-Date: 2016-05-26 10:44:16
Implementation-Version: 1.0.0-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: 
Specification-Vendor: my org
Repo-Name: model
Specification-Title: model
Implementation-Vendor-Id: com.org
Git-Hash: xxx
Implementation-Vendor: my org
App-Name: model
Version-Number: 1.0.0-SNAPSHOT
Created-By: Apache Maven 3.0.5
Build-Jdk: 1.8.0_40
Specification-Version: 1.0.0-SNAPSHOT

查看有关 groovy 编译器的 old thread

Are you by any chance explicitly specifying 'javac' as an executable somewhere in your pom? The only way that I can reproduce your problem is by using a configuration like this:

<configuration>
  <compilerId>groovy-eclipse-compiler</compilerId>
  <executable>javac</executable>
  <fork>true</fork>
</configuration>

When I remove the {{javac}} option, then things work fine.

This is a small bug in the groovy-eclipse-compiler since we should never be looking at the executable option (and I'll fix this), but the workaround is easy.

this old one

Now fixed locally. Please re-open if this is not the problem that you were having.

When using the Groovy Eclipse Compiler within a Maven build, setting maven-compiler-plugin option forked = true causes "invalid flag: -jar" error

这个问题似乎与 forkexecutable 的使用有关,即使它涉及旧版本的 groovy 编译器,它仍然(部分? ) 那里。

使用下面的最小 POM:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>sample-project</artifactId>
    <version>0.0.2-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <compilerId>groovy-eclipse-compiler</compilerId>
                    <source>1.7</source>
                    <target>1.7</target>
                    <showDeprecation>true</showDeprecation>
                    <showWarnings>true</showWarnings>
                    <verbose>true</verbose>
                    <executable>path_to_java8\bin\javac.exe</executable>
                    <fork>true</fork>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>2.9.2-01</version>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>2.4.3-01</version>
                    </dependency>
                    <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.11</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

我们已经可以重现问题和 误导性的 无效标志错误。

删除 <fork>true</fork> 配置条目,构建将是 SUCCESSFUL,但它也会跳过 executable 元素,根据官方文档,该元素用于

Sets the executable of the compiler to use when fork is true.


解决此问题的建议是:

  • 删除 forkexecutable 选项
  • 使用主要 Java 版本,在本例中 8 作为所有模块的默认 JDK
  • 根据需要将 source/target 设置为次要版本,在这种情况下 7 在某些模块中
  • 在相关模块中相应地配置 animal-maven-sniffer 以确保您遵守 Java 7 交叉编译。

关于交叉编译,我也强烈推荐阅读这个

问题 "javac: invalid flag: -jar" 是 groovy-eclipse-compiler 预期 java 的路径而不是 "executable" 标签中的 javac

  <configuration>
      <compilerId>groovy-eclipse-compiler</compilerId>
      <source>${java.source.version}</source>
      <target>${java.target.version}</target>
      <showDeprecation>true</showDeprecation>
      <showWarnings>true</showWarnings>
      <verbose>true</verbose>
      <executable>${jdk}/bin/javac</executable>
      <fork>true</fork>
  </configuration>

你可以替换 <executable>${jdk}/bin/javac</executable> <executable>${jdk}/bin/java</executable>

希望对您有所帮助

这种事情的最佳解决方案是使用Toolchain