简体   繁体   English

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

[英]Mitigating verbosity of Maven pom.xml files (or: a critique of Maven by a fan)

I like maven. 我喜欢maven。 I even like it very very much. 我非常喜欢它。 Since I switched to it from Ant I have saved lots of hours of works, building build files, administrating dependencies, etc, and saved lots of space in my source control repository. 自从我从Ant切换到它以来,我已经节省了大量的工作时间,构建构建文件,管理依赖项等,并在我的源代码控制存储库中节省了大量空间。

The problem is that maven files are too verbose. 问题是maven文件太冗长了。 Not that Ant files where less verbose, but their verbosity was appropriate for what they do. 并不是说Ant文件不那么冗长,但它们的冗长程度适合他们的工作。

For example, instead of writing : 例如, 而不是写

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

I would like to write something like 想写点类似的东西

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

Or instead of 或者代替

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

I would like 我想要

<build version="1.5"/>

And (last example and we are done), instead of writing : 并且(最后一个例子,我们已经完成), 而不是写

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

I would like not to have to write nothing. 我不想写任何东西。 Ie, maven would detect the presence of the native2ascii folder and do the right thing by default. 即,maven将检测native2ascii文件夹的存在,并默认做正确的事情

I know I am mixing built-in functionality with plugins and other things, but please try to look from the point of view of a maven user, who is very happy with the tool, but thinks he could be happier. 我知道我正在将内置功能与插件和其他东西混合使用,但请尝试从对该工具非常满意的maven用户的角度来看,但认为他可以更开心。

So: 所以:

  1. Is there a way I can configure maven to work like this? 有没有办法让maven像这样工作? (And would it be wise to do so) (这样做是否明智)

  2. Is there perhaps another tool I am not aware of that does this? 是否有另一种我不知道的工具呢?

Maven configuration is certainly verbose, Maven 3 aims to address this amongst other things (see these videos for some idea), and there is a plugin for Maven 2 that allows for configuration to be defined in YAML. Maven配置肯定是冗长的,Maven 3旨在解决这个问题(参见这些视频的一些想法),并且有一个Maven 2插件允许在YAML中定义配置。 There's also an experimental "terse" branch that supports attributes, reducing the size of the configuration somewhat. 还有一个实验性的“简洁”分支 ,它支持属性,在某种程度上减少了配置的大小。

For example: 例如:

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

With Maven 2, you can mitigate the verbosity by defining common configuration in a parent project, in the examples you cite, the dependencies can all be defined in the parent, or in the dependencyManagement section of a parent so a child can declare the junit dependency as 使用Maven 2,您可以通过在父项目中定义公共配置来缓解详细程度,在您引用的示例中,依赖项都可以在父项中定义,或者在父项的dependencyManagement部分中定义,以便子项可以声明junit依赖项如

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

which is a bit of an improvement. 这有点改善。

The plugins can both be declared in the parent and need not be defined in your child. 插件既可以在父级中声明,也不需要在子级中定义。

I completely agree with you that some parts of the pom.xml may be condensed, in particular the <dependencies> part. 我完全同意你的观点,pom.xml的某些部分可能会被压缩,特别是<dependencies>部分。

I really like the Ivy way of declaring dependencies : 我非常喜欢Ivy声明依赖关系的方式:

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

I've seen a blog post that proposed an experimental tool to create the dependencies as Ivy do. 我看过一篇博文 ,提出了一个实验工具来创建依赖项,就像Ivy那样。 However, I've never tried it. 但是,我从未尝试过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM