【问题标题】:Mitigating verbosity of Maven pom.xml files (or: a critique of Maven by a fan)减轻 Maven pom.xml 文件的冗长(或:一个粉丝对 Maven 的批评)
【发布时间】:2010-11-26 10:21:58
【问题描述】:

我喜欢 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> 

我想写一些类似

的东西
<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. 是否有其他我不知道的工具可以做到这一点?

【问题讨论】:

  • + 1 表示您的问题的良好精神:)

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


【解决方案1】:

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,您可以通过在父项目中定义通用配置来减轻冗长,在您引用的示例中,依赖项都可以在父项中定义,或者在父项的 dependencyManagement 部分中定义,以便子项可以声明junit 依赖为

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

这是一个进步。

插件既可以在父级中声明,也无需在您的子级中定义。

【讨论】:

    【解决方案2】:

    我完全同意你的观点,pom.xml 的某些部分可能会被压缩,尤其是 &lt;dependencies&gt; 部分。

    我真的很喜欢 Ivy 声明依赖项的方式:

    <dependencies>
        <dependency org="commons-lang" name="commons-lang" rev="2.0"/>
        ...
    

    我看到blog post 提出了一个实验性工具来像 Ivy 一样创建依赖项。但是,我从未尝试过。

    【讨论】:

    • 如果只有 Maven 人费心去正确地了解 XML,尤其是“属性”在它们开始之前就已经存在......甚至不要让我开始了解 。跨度>
    猜你喜欢
    • 1970-01-01
    • 2013-11-29
    • 2022-07-19
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2011-03-03
    • 1970-01-01
    相关资源
    最近更新 更多