java - 减轻 Maven pom.xml 文件(或 : a critique of Maven by a fan)

标签 java maven-2 project-management build-process

我喜欢专家。我什至非常非常喜欢它。 自从我从 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> 

我想写类似的东西

<dependencies>
    commons-logging/commons-logging/1.1.1
    com.myosproject/superlibrary/7.5
    test:junit/junit/3.8.1
</dependencies>

或者代替

<build>
    <plugins>
        <plugin>
             <artifactId>maven-compiler-plugin</artifactId>
                 <configuration>
                     <source>1.5</source>
                     <target>1.5</target>
                 </configuration>
        </plugin>
    </plugins>

我愿意

<build version="1.5"/>

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

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

我不想什么都不写。即,maven 会检测 native2ascii 文件夹的存在并默认执行正确的操作

我知道我将内置功能与插件和其他东西混合在一起,但请尝试从 maven 用户的角度来看,他们对这个工具非常满意,但认为他可以更快乐。

所以:

  1. 有没有一种方法可以配置 maven 使其像这样工作? (这样做是否明智)

  2. 是否有其他我不知道的工具可以做到这一点?

最佳答案

Maven 配置肯定是冗长的,Maven 3 旨在解决这个问题(请参阅 these videos 了解一些想法),并且有一个 plugin for Maven 2允许在 YAML 中定义配置。还有一个 experimental "terse" branch支持属性,稍微减少了配置的大小。

例如:

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

使用 Maven 2,您可以通过在父项目中定义公共(public)配置来减轻冗长,在您引用的示例中,依赖项都可以在父项目中定义,或者在父项目的 dependencyManagement 部分中定义,以便子项目可以声明junit 依赖为

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
</dependency>

这有点改进。

插件都可以在父级中声明,不需要在您的子级中定义。

关于java - 减轻 Maven pom.xml 文件(或 : a critique of Maven by a fan),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1392649/

相关文章:

java - : Allow only letters and spaces but not start with space? 的正则表达式是什么

java - CopyOnWriteArrayList 上线程中的迭代器不起作用

java - 我如何在java中使用splitter ^

maven-2 - maven 显示一些标记为 "unnamed"的模块

documentation - 软件文档模板

Java Abacus,使用 for 循环绘制计数器时遇到问题

unit-testing - 有没有人成功地使用 Jboss 嵌入式、Seam 和 Maven 运行集成测试?

java - 在 Maven 中生成一个 Version.java 文件

project-management - 理解未注释和复杂项目的最佳或最快方式

java - Python 缺乏静态类型如何影响大型项目的可维护性和可扩展性?