开发者

default maven compiler setting

Right now, I'm writing a small java application by my own, with few maven pom.xml files. I want to mak开发者_StackOverflowe 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

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

Is there a simpler way to resolve this issue?


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

Put a parent declaration at the top of your pom files:

<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 dependencies and plugins in your parent pom file. Usually you'll want to favor dependencyManagement and pluginManagement. See the documentation for more details.


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

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.xml values as you.


I'm sick of copy-and-pasting

Yes, and you should use POM inheritance to avoid this and configure the maven-compiler-plugin in the parent 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).

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

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


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

If this is multi-module project just put these settings to top-level POM under 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜