2016-05-26 77 views
2

我正在嘗試構建一個maven multimodule項目,我希望在java 8上構建兩個模塊,其中一個構建在java上7maven多模塊項目,用不同的java版本編譯模塊之一

編輯:我也看了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.迪馬特奧回答我嘗試

  1. 從maven-compiler-plugin的config部分刪除fork和可執行選項
  2. 設置源和目標版本1.7我的模塊上需要一個不同版本的Java

的它成功生成。但是當我讀取構建清單時,它說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 
+0

「invalid flag:-jar」和不能切換javac版本之間的關係是什麼?這聽起來像是一個不同的錯誤。你怎麼知道Java 8仍然在使用? – Tunaki

+0

@Tunaki所以我只是報告我收到的錯誤,我不知道這個關係是什麼 –

+0

「Build-JDK」是用於編譯的JDK,而不是它編譯的目標版本,所以你不要擔心這一點,它與你使用的是一致的。請按照下面的註釋中的解釋使用'javap'工具進行檢查。 –

回答

2

望着這old thread關於Groovy編譯:

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

好像這個問題是關係到fork使用和executable,儘管它涉及到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,但它也將跳過,其從正式文件,用於

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


有人建議然後克服所述executable元件問題是:

  • 取出forkexecutable選項
  • 使用主流的Java版本,在這種情況下作爲默認JDK所有模塊
  • source/target到所需的次要版本,在這種情況下,某些模塊
  • 配置animal-maven-sniffer因此在相關模塊中確保您遵守Java 7交叉編譯。

關於交叉編譯,我也高度推薦閱讀這StackOverflow post

+0

非常感謝,但我如何驗證未成年人是1.7? –

+0

使用在java 1.8中可用的任何字符串實用程序函數,但不在1.7 –

+0

快速檢查將從'target \ classes'目錄運行'javap -verbose com.sample.MyClass',並在輸出中檢入Java版本在數字格式中,Java 7將在命令輸出 –

0

問題 「的javac:無效的標誌:罐子」 是,Groovy-Eclipse的編譯器預期路徑到Java不要在 「可執行文件」 標籤的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/java</executable>

取代 <executable>${jdk}/bin/javac</executable>

希望這有助於

相關問題