减轻Maven pom.xml文件的冗长(或者:粉丝批评Maven)

fly*_*ire 15 java project-management build-process maven-2

我喜欢maven.我非常喜欢它.自从我从Ant切换到它以来,我已经节省了大量的工作时间,构建构建文件,管理依赖项等,并在我的源代码控制存储库中节省了大量空间.

问题是maven文件太冗长了.并不是说Ant文件不那么冗长,但它们的冗长程度适合他们的工作.

例如,而不是写:

<dependencies>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
<dependency>
    <groupId>com.myosproject</groupId>
    <artifactId>superlibrary</artifactId>
    <version>7.5</version>
</dependency> 
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>
</dependencies> 
Run Code Online (Sandbox Code Playgroud)

想写点类似的东西

<dependencies>
    commons-logging/commons-logging/1.1.1
    com.myosproject/superlibrary/7.5
    test:junit/junit/3.8.1
</dependencies>
Run Code Online (Sandbox Code Playgroud)

或者代替

<build>
    <plugins>
        <plugin>
             <artifactId>maven-compiler-plugin</artifactId>
                 <configuration>
                     <source>1.5</source>
                     <target>1.5</target>
                 </configuration>
        </plugin>
    </plugins>
Run Code Online (Sandbox Code Playgroud)

我想要

<build version="1.5"/>
Run Code Online (Sandbox Code Playgroud)

并且(最后一个例子,我们已经完成),而不是写:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>native2ascii-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>native2ascii</goal>
            </goals>
            <configuration>
            <encoding>UTF8</encoding>
                </configuration>
        </execution>
     </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

我不想写任何东西.即,maven将检测native2ascii文件夹的存在,并默认做正确的事情.

我知道我正在将内置功能与插件和其他东西混合使用,但请尝试从对该工具非常满意的maven用户的角度来看,但认为他可能会更快乐.

所以:

  1. 有没有办法让maven像这样工作?(这样做是否明智)

  2. 是否有另一种我不知道的工具呢?

Ric*_*ler 9

Maven配置肯定是冗长的,Maven 3旨在解决这个问题(参见这些视频的一些想法),并且有一个Maven 2插件允许在YAML中定义配置.还有一个实验性的"简洁"分支,它支持属性,在某种程度上减少了配置的大小.

例如:

groupId: org.twdata.maven
artifactId: maven-yamlpom-plugin
version: 1.0-SNAPSHOT
packaging: maven-plugin
name: YAML POM Plugin
dependencies:
  - { groupId: org.apache.maven, artifactId: maven-plugin-api, version: 2.0 }
  - { groupId: SnakeYAML, artifactId: SnakeYAML, version: 1.1 }
  - { groupId: commons-io, artifactId: commons-io, version: 1.4 }
  - { groupId: dom4j, artifactId: dom4j, version: 1.4 }
  - { groupId: junit, artifactId: junit, version: 3.8.1, scope: test }
  - { groupId: xmlunit, artifactId: xmlunit, version: 1.2, scope: test }
build:
  plugins:
    - artifactId: maven-compiler-plugin
      configuration:
    source: 1.5
    target: 1.5
repositories:
  - id: snakeyaml
    name: SnakeYAML repository
    url: http://snakeyamlrepo.appspot.com/repository
Run Code Online (Sandbox Code Playgroud)

使用Maven 2,您可以通过在父项目中定义公共配置来缓解详细程度,在您引用的示例中,依赖项都可以在父项中定义,或者在父项的dependencyManagement部分中定义,以便子项可以声明junit依赖项如

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这有点改善.

插件既可以在父级中声明,也不需要在子级中定义.