Java 默认 Maven 编译器设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2531650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:54:32  来源:igfitidea点击:

default maven compiler setting

javacompiler-constructionmaven

提问by Jeeyoung Kim

Right now, I'm writing a small java application by my own, with few maven pom.xml files. I want to make all my maven packages to compile with jdk 1.6, and I can't find a good way to do it without manually setting it on every single POMs - I'm sick of copy-and-pasting

现在,我正在自己编写一个小型 java 应用程序,其中很少有 maven pom.xml 文件。我想让我所有的 maven 包都用 jdk 1.6 编译,如果没有在每个 POM 上手动设置它,我找不到一个好的方法 - 我厌倦了复制和粘贴

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
 <source>1.6</source>
 <target>1.6</target>
</configuration>

in every single pom.xml file I generate.

在我生成的每个 pom.xml 文件中。

Is there a simpler way to resolve this issue?

有没有更简单的方法来解决这个问题?

采纳答案by leedm777

Create a pom-only (<packaging>pom</packaging>) project that has the compiler settings (and any other default settings) you want. You give treat it like any other project (release it; deploy it to your Maven repo, etc.).

创建一个 pom-only ( <packaging>pom</packaging>) 项目,该项目具有您想要的编译器设置(和任何其他默认设置)。您可以像对待任何其他项目一样对待它(发布它;将其部署到您的 Maven 存储库等)。

Put a parentdeclaration at the top of your pom files:

parent在 pom 文件的顶部放置一个声明:

<parent>
  <groupId><!-- parent's group id --></groupId>
  <artifactId><!-- parent's artifact id --></artifactId>
  <version><!-- parent's version --></version>
</parent>

It doesn't help much if all you want to set is compiler settings. But if you find yourself configuring lots of plugins, reports and dependencies in the same way across project, you can create one parent to rule them all.

如果您只想设置编译器设置,那没有多大帮助。但是,如果您发现自己在整个项目中以相同的方式配置了大量插件、报告和依赖项,则可以创建一个父项来管理所有这些。

BTW - be careful about declaring dependenciesand pluginsin your parent pom file. Usually you'll want to favor dependencyManagementand pluginManagement. See thedocumentationfor more details.

顺便说一句 -在你的父 pom 文件中声明dependencies和小心plugins。通常你会想要偏爱dependencyManagementpluginManagement。请参见文档了解更多详情。

回答by matt b

You could specify this plugin and configuration in your ~/.m2/settings.xml, which will then apply it to all projects.

您可以在您的 中指定此插件和配置,~/.m2/settings.xml然后将其应用于所有项目。

However this has the downside of making your projects no longer portable - attempting to build the same code with the same pom.xml will fail on other machines that don't have the same settings.xmlvalues as you.

然而,这样做的缺点是使您的项目不再可移植 - 尝试使用相同的 pom.xml 构建相同的代码将在其他settings.xml与您没有相同值的机器上失败。

回答by Pascal Thivent

I'm sick of copy-and-pasting

我厌倦了复制粘贴

Yes, and you should use POM inheritanceto avoid this and configure the maven-compiler-pluginin the parent POM.

是的,您应该使用 POM继承来避免这种情况并maven-compiler-plugin在父 POM 中配置。

Another option would be to use the solution suggested by @matt(and he nailed down pros and cons of the use of settings.xml).

另一种选择是使用@matt建议的解决方案(他明确了使用 的优缺点settings.xml)。

In both cases, this is typically a setting that I like to check using the maven-enforcer-pluginand its requireJavaVersionrule that you would configure like this:

在这两种情况下,这通常是我喜欢使用maven-enforcer-plugin和它的requireJavaVersion规则来检查的设置,您将像这样配置:

<project>
  [...]
  <build>
   <plugins>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <executions>
          <execution>
            <id>enforce-versions</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireJavaVersion>
                  <version>1.6</version>
                </requireJavaVersion>
              </rules>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

But it can do more (like checking the maven version). Very useful.

但它可以做更多(比如检查 Maven 版本)。很有用。

回答by kopper

I want to make all my maven packages to compile with jdk 1.6

我想让我所有的 maven 包都用 jdk 1.6 编译

If this is multi-module project just put these settings to top-level POM under pluginManagement.

如果这是多模块项目,只需将这些设置放在pluginManagement.

If you have many independent project just copy-and-paste this configuration. Beware of "smart" solutions like setting this somewhere globally. Some day you will want to use different compiler settings for one or two of your projects and the nightmare will begin :-)

如果您有许多独立项目,只需复制并粘贴此配置。当心“智能”解决方案,例如在全球某个地方设置它。有一天你会想要为你的一两个项目使用不同的编译器设置,噩梦就会开始:-)

Remember...

记住...

Keep things as simple as possible, but no simpler.

让事情尽可能简单,但不要更简单