当前位置: 首页 > 面试题库 >

适用于测试和主要版本的不同Maven编译器版本

夏谦
2023-03-14
问题内容

如何配置Maven编译器以将Java 5用于测试代码并将Java 1.4用于主代码?


问题答案:

如果要设置对相关Java版本的遵从性,则可以为每次执行配置编译器插件。假设Maven使用的JDK至少与您指定的最高版本相同。通过使用属性,可以在命令行或子项中覆盖该配置(如果需要):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>${compileSource}</source>
    <target>${compileSource}</target>
  </configuration>
  <executions>
    <execution>
      <id>test-compile</id>
      <phase>process-test-sources</phase>
      <goals>
        <goal>testCompile</goal>
      </goals>
      <configuration>
        <source>${testCompileSource}</source>
        <target>${testCompileSource}</target>
      </configuration>
    </execution>
  </executions>
</plugin>
...
<properties>
  <compileSource>1.4</compileSource>
  <testCompileSource>1.5</testCompileSource>
</properties>

如果您打算使用其他 编译器
,则涉及的更多。因为您需要指定JDK的路径以及所使用的编译器版本。同样,这些可以在属性中定义。尽管您可能想在settings.xml中定义它们

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>${compileSource}</source>
    <target>${compileSource}</target>
    <executable>${compileJdkPath}/bin/javac</executable>
    <compilerVersion>${compileSource}</compilerVersion>
  </configuration>
  <executions>
    <execution>
      <id>test-compile</id>
      <phase>process-test-sources</phase>
      <goals>
        <goal>testCompile</goal>
      </goals>
      <configuration>
        <source>${testCompileSource}</source>
        <target>${testCompileSource}</target>
        <executable>${testCompileJdkPath}/bin/javac</executable>
        <compilerVersion>${testCompileSource}</compilerVersion>
      </configuration>
    </execution>
  </executions>
</plugin>
...
<properties>
  <compileSource>1.4</compileSource>
  <testCompileSource>1.5</testCompileSource>
  <compileJdkPath>path/to/jdk</compileJdkPath>
  <testCompileJdkPath>path/to/test/jdk<testCompileJdkPath>
</properties>

注意,在配置文件中定义编译器配置可能是有意义的,对于您支持的每个JDK,一个配置文件都可以使用,以便您的常规构建不依赖于所设置的属性。

另外, 在Maven 3.x中,fork在指定可执行文件时需要包括参数,例如:

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <executions>
      <execution>
        <id>default-testCompile</id>
        <phase>test-compile</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
        <configuration>
          <fork>true</fork>
          <executable>${testCompileJdkPath}/bin/javac</executable>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>            
      </execution>
    </executions>
  </plugin>


 类似资料:
  • 我正在更新一个Spigot(Minecraft)插件,最新版本的Spigot需要Java16。在我的pom中,我将maven编译器插件目标更改为16,源代码仍然是1.8。现在我得到以下错误: POM: 4.0.0

  • 我使用android Studio制作了一个静态编程语言MultiPlatform Mobile项目,导入SQLDelight后遇到以下错误: 我回答了这个问题,但他们提到的一切我都做对了。在一个文件一个文件地查看之后,我没有找到我在哪里声明了那个版本,于是我决定将我的项目与其中一个示例项目进行比较。 再次查看这些文件后,我发现我在我的应用程序等级中使用了SQLDelightVersion1.5.

  • 我的谷歌登录非常适合调试变体。但是当我尝试构建发布变体时,它停止工作......我提到了这个答案,但就我而言,我还没有上传到我的应用程序到Play商店。那么在这种情况下,我如何获得新的 sha1 密钥呢?

  • 我在maven-compiler-plugin的pom.xml中有以下配置。 JDK的源版本和目标版本应该是什么?它如何依赖于安装在我的计算机上的jdk版本?他们可能不一样吗?例如,安装的jdk是1.8,在源参数-1.6,目标-1.7中。

  • 问题内容: 我有一个与设备固件通信的应用程序。由于固件发生更改,因此使用格式对其进行了版本控制。举几个例子,当前版本是,之后是,有时是。不幸的是,固件的版本控制格式不受我的控制,因此我无法更改它。 我需要一种相互比较固件的方法。基本上我需要一个功能 到目前为止,我所做的就是将这种格式转换为简单格式。因此将变为(每个级别2位数字)。问题是 我的代码难以阅读(> 40行和3种方法) 我敢肯定,对此有一

  • 带Spring2.1.3。版本,H2集成测试运行良好。 以下是配置 应用程序属性: 配置类: 引导: 将Spring boot版本升级到2.3.12.release.后,上述配置停止工作 我们看到的错误是 原因:组织。h2.jdbc。JdbcSQLSyntaxErrorException:未找到表“USERPROFILE”;SQL语句:插入userProfile。。。。。 当测试用例开始时,下面是