maven多模块项目,使用不同的java版本编译其中一个模块

时间:2016-05-26 14:07:58

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

我正在尝试构建一个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

EDIT ::

所以基于A. Di Matteo回答我试过

  1. 从maven-compiler-plugin的配置部分删除fork和可执行选项
  2. 在我的模块上将源版本和目标版本设置为1.7,需要不同版本的java
  3. 它构建成功。但是当我阅读构建清单时,它说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
    

3 个答案:

答案 0 :(得分:2)

关于groovy编译器的这个old thread

  

您是否有机会明确指定&#39; javac&#39;作为pom中某处的可执行文件?我可以重现您的问题的唯一方法是使用这样的配置:

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

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

     

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

this old one

  

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

     

在Maven构建中使用Groovy Eclipse编译器时,设置maven-compiler-plugin选项forked = true会导致&#34;无效标志:-jar&#34;错误

似乎这个问题与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元素,从官方文档中,该元素用于

  

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

然后克服这个问题的建议是:

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

关于交叉编译,我还建议您阅读此StackOverflow post

答案 1 :(得分:0)

问题&#34; javac:无效标志:-jar&#34;是groovy-eclipse-compiler期望java的路径,而不是javac in&#34; executable&#34;标签

  <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>

希望这有帮助

答案 2 :(得分:0)

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