【问题标题】:maven multi module project, compile one of the modules with a different java versionmaven多模块项目,编译不同java版本的模块之一
【发布时间】:2016-09-24 14:23:59
【问题描述】:

我正在尝试构建一个 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 版本的模块上将源和目标版本设置为 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

【问题讨论】:

  • "invalid flag: -jar" 和无法切换 javac 版本有什么关系?这听起来像是一个不同的错误。你怎么知道 Java 8 还在被使用?
  • @Tunaki 所以我只是报告我遇到的错误,我不确定关系是什么
  • "Build-JDK" 是用于编译的 JDK,而不是编译它的目标版本,所以你不必担心,它与你使用的一致。请使用javap 工具进行检查,如下面的 cmets 所述。

标签: java maven maven-3 pom.xml maven-compiler-plugin


【解决方案1】:

看看这个关于 groovy 编译器的old thread

您是否有任何机会在您的 pom 中明确指定“javac”作为可执行文件?我可以重现您的问题的唯一方法是使用这样的配置:

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

当我删除 {{javac}} 选项时,一切正常。

这是 groovy-eclipse-compiler 中的一个小错误,因为我们永远不应该查看可执行选项(我会修复这个问题),但解决方法很简单。

还有this old one

现在已在本地修复。如果这不是您遇到的问题,请重新打开。

在 Maven 构建中使用 Groovy Eclipse 编译器时,设置 maven-compiler-plugin 选项 forked = true 会导致“invalid flag: -jar”错误

问题似乎与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>

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

删除&lt;fork&gt;true&lt;/fork&gt; 配置条目,构建将是SUCCESSFUL,但它也会跳过官方文档中用于的executable 元素

设置当forktrue 时使用的编译器的可执行文件。


然后解决该问题的建议是:

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

关于交叉编译,我也强烈建议阅读StackOverflow post

【讨论】:

  • 非常感谢,但我如何验证未成年人是为 1.7 构建的??
  • 使用任何在 java 1.8 中可用但在 1.7 中不可用的字符串实用函数
  • 快速检查是从target\classes 目录运行javap -verbose com.sample.MyClass 并以数字格式检查输出Java 版本,Java 7 将在命令输出的顶部为51跨度>
  • 谢谢,是的,它显示了次要版本:0 主要版本:51
【解决方案2】:

问题 "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>

你可以替换 &lt;executable&gt;${jdk}/bin/javac&lt;/executable&gt; &lt;executable&gt;${jdk}/bin/java&lt;/executable&gt;

希望对你有帮助

【讨论】:

    【解决方案3】:

    此类事情的最佳解决方案是使用Toolchain

    【讨论】:

      猜你喜欢
      • 2012-09-07
      • 2012-07-05
      • 2012-05-13
      • 2013-12-03
      • 2011-08-09
      • 2021-02-25
      • 1970-01-01
      • 2016-06-21
      相关资源
      最近更新 更多