繁体   English   中英

Maven 编译不同JDK版本

[英]Maven compiling with different JDK versions

我的应用程序在 Java 1.6u45 和 Java 1.8 上的用户之间拆分。 我们的问题是我们不能同时指定项目系统库和代码有两个不同的编译器设置。

项目结构:

Project >
    > src/main/java/com/us/javafx/... (Java 8 code)
    > src/main/java/com/us/... (Java 6 code)
    > src/main/resources/...

我们的 POM.xml:

<?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.us</groupId>
   <artifactId>PROJECT</artifactId>
   <version>1.0.0</version>
   <packaging>jar</packaging>
   <name>PROJECT</name>
   <description>DESCR</description>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

</dependencies>

<build>
    <plugins>
        <!-- Copy all dependencies to jars directory -->
          <plugin> 
              <artifactId>maven-dependency-plugin</artifactId> 
                  <executions> 
                      <execution> 
                          <phase>package</phase> 
                          <goals> 
                              <goal>copy-dependencies</goal> 
                          </goals> 
                          <configuration> 
                              <outputDirectory>${project.build.directory}/jars</outputDirectory> 
                          </configuration> 
                      </execution> 
                  </executions> 
          </plugin>


            <!-- Maven Compiler -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>  
                <configuration>
                    <!-- Necessary to avoid setting JRE on Maven project update! -->
                    <!-- SEE PHOTO FOR WHY THIS IS HERE -->
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>            
                <executions>

                    <!-- Compile Java 6 directory -->
                    <execution>
                        <id>java6</id>
                        <configuration>
                            <excludes>
                                <exclude>com/us/javafx/**/*</exclude>
                            </excludes>
                            <!-- Non working attempt to set compiler version -->
                            <source>1.6</source>
                            <target>1.6</target>
                        </configuration>
                    </execution>

                    <!-- Compile Java 8 directory -->
                    <execution>
                        <id>java8</id>
                        <configuration>
                            <includes>
                                <include>com/us/javafx/**/*</include>
                            </includes>
                            <!-- Non working attempt to set compiler version -->
                            <source>1.8</source>
                            <target>1.8</target>
                        </configuration>
                    </execution>

                </executions>
            </plugin>

            <!-- Create JAR in jars folder -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <outputDirectory>${project.build.directory}/jars</outputDirectory> 
                </configuration>
              </plugin>

        </plugins>
</build>

如果我们告诉 Maven 编译器插件使用 Java 1.8,Java 1.6 用户得到不支持的主要版本错误。 Or we tell the Maven compiler plugin to use Java 1.6 but our Java 8 code will complain about errors when performing Right Click Project > Maven > Update Project... (which causes the screen shot below):

在此处输入图像描述

有没有一种方法可以同时实现:

1)用各自的编译器编译Java 6和8代码

2)选择Java 8作为我们项目的JRE系统库

我最近了解了toolchains.xml。 Maven甚至记录了该文档,并从2.0.9开始支持它! 请参阅工具链文档

因此,我向我的〜/ .m2 /文件夹中添加了一个toolchain.xml文件,内容如下:

<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
 <!-- JDK toolchains -->
 <toolchain>
   <type>jdk</type>
   <provides>
     <version>1.8</version>
     <vendor>sun</vendor>
   </provides>
   <configuration>
     <jdkHome>/opt/java8</jdkHome>
   </configuration>
 </toolchain>
 <toolchain>
   <type>jdk</type>
   <provides>
     <version>1.7</version>
     <vendor>sun</vendor>
   </provides>
   <configuration>
     <jdkHome>/opt/java7</jdkHome>
   </configuration>
 </toolchain>
</toolchains>

它使您可以定义Maven可以使用哪些不同的JDK来构建项目,而与运行JDK的Maven无关。 在IDE中在项目级别定义JDK时,有点像。 当我需要使用Java 7构建另一个项目同时将Java 8作为默认项目时,就使用了它。

我想这可以解决您的问题。

Maven 配置文件是最简单和最有效的配置文件。 您也可以从 IDE 中选择和运行它们。 您为所需的每个不同 JDK 设计一个配置文件。 您放入每个不同的 JDK 配置文件:

  • 源代码目标等属性
  • 专用于该配置文件的依赖项。
Project
    +properties
    +dependencies
    +Profiles
        Profile JDK8
            +jdk8-properties
                jdk.version
                maven.compiler.source
                maven.compiler.target   
            +jdk8-dependencies
        Profile JDK11
            +jdk11-properties
                jdk.version
                maven.compiler.source
                maven.compiler.target   
            +jdk11-dependencies

maven 配置文件树的图片

还有一个例子

<project>
    <name>x</name>
    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>2.1.210</version>
        </dependency>
    </dependencies>

    <profiles>
        <!-- different Java versions -->
        <profile>
            <id>jdk8</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <jdk.version>1.8</jdk.version>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core-jakarta</artifactId>
                    <version>5.6.10.Final</version>
                </dependency>
                <!-- needed if jakarta is used -->
                <dependency>
                    <groupId>javax.enterprise</groupId>
                    <artifactId>cdi-api</artifactId>
                    <version>2.0</version>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>org.postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>42.2.25</version>
                </dependency>
            </dependencies>
        </profile>

        <profile>
            <id>jdk11</id>
            <properties>
                <jdk.version>11</jdk.version>
                <maven.compiler.source>11</maven.compiler.source>
                <maven.compiler.target>11</maven.compiler.target>
            </properties>

            <dependencies>
                <dependency>
                    <groupId>org.hibernate.orm</groupId>
                    <artifactId>hibernate-core</artifactId>
                    <version>6.1.2.Final</version>
                </dependency>
                <dependency>
                    <groupId>org.postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>42.4.2</version>
                </dependency>

            </dependencies>
        </profile>
    </profiles>
    <properties>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <junit-version>4.13.2</junit-version>
    </properties>
</project>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM