是否存在生成POM.xml文件的Java类? [英] Is there a Java Class that generates a POM.xml file?

查看:205
本文介绍了是否存在生成POM.xml文件的Java类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的工作项目中,最近有人要求我通过java类生成POM文件.问题在于,我对Maven非常非常陌生(例如去年12月以来).

In my job project I have recently been asked to generate POM files via a java class. The problem is that I am very, very new to Maven (like since last December).

我需要的是给出所有必要数据的,以某种方式生成xml文件(一个pom文件,但是如果我可以配置任何可以创建XML的XML的代码)的代码,那么他们就不需要编写该文件了.手.我不知道自己是否在解释自己,但问题是,是否存在任何使用给定数据生成或构造POM文件的库或类? 如果不是很好,我只是不想浪费更多时间寻找不知道它是否存在的东西,或者只是声明一个POM对象那么简单,然后做一个琐碎的Document d = generatePom(POM p).由于没有人抱怨编写POM文件有多困难,我认为应该有一种简单的方法来处理它们,但是我认为我已经迷失了很多API Javadoc,无法找到回头路.

What I need is a somehow code that generates a xml file (a pom file, but if I can configure any xml creating code that will be fine) given all the necesary data, so they don't need to write it by hand. I don't know if I am explaining myself, but the question is, is there any library or class that generates or constructs a POM file with a given data? If not that is just fine, I just don't want to loose more time searching for something I don't know if it even exists or if is as simple as declaring a POM object and then doing a trivial Document d = generatePom(POM p). Since nobody is complaining of how hard is writing POM files I supose there should be an easy way to do them but I think I have lost myself in a lot of API javadoc and I can't find my way back.

如果没有代码,我的想法是使用以下方法搜索POM词典(以覆盖所有元素)并使用给定的POM对象(我之前已经填充了提供的数据)创建xml文件. XML生成器,例如JDOM,XOM或XStream.如果没有任何班级对此事有任何想法,将不胜感激(例如嘿!您正在做错了").

My idea if there is no code for this is to search for the POM dictionary (to cover all elements) and create a xml file with a given POM object (that I had previously filled with the data I am provided), using an XML generator such as JDOM, XOM or XStream. Any thoughts about this would be appreciated if there is no class that already does this (like 'hey! you are doing it WRONG').

PS:我已经读到Eclipse项目正在做一些Maven事情,并且它具有一个API,该API会为您拥有的实际项目生成pom.xml文件.如果我可以覆盖输入数据或其他内容,那将是一件好事.

PS: I have read that the Eclipse project is doing some Maven things and that has an API that generates a pom.xml file for the actual project you have. That would be a great thing if I could override the input data or something.

谢谢大家!

推荐答案

这取决于您要执行的操作.如果您只想为肯定类型的新项目创建POM,最好的方法是通过Maven原型(您可以使用所需的模板创建自己的原型).

It depends on what you are trying to do. If you just want to create POMs for new projects of a certainly type, the best way is through Maven archetypes (you can create your own archetypes with the templates you want).

如果您确实需要以编程方式编写POM,则可以使用以下内容:

If you really have a need to programmatically write a POM, you can use the following:

import org.apache.maven.model.*;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
...
Model model = new Model();
model.setGroupId( "some.group.id" );
...
new MavenXpp3Writer().write( w, model );

...其中w是一个java.io.Writer,您添加了所有必要的异常处理.

... where w is a java.io.Writer and you add all the necessary exception handling.

Javadoc在这里: http://maven .apache.org/ref/2.2.1/maven-model/apidocs/index.html

The Javadoc is here: http://maven.apache.org/ref/2.2.1/maven-model/apidocs/index.html

要访问此API,您应该添加此依赖项:

To access this API, you should add this dependency:

<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-model</artifactId>
  <version>2.2.1</version>
</dependency>

也有相应的读取API,但是请记住,它不会执行所有Maven操作,例如继承和插值(要做到这一点需要更高级的API使用).

There is a corresponding read API as well, but bear in mind that it won't do all the Maven operations such as inheritence and interpolation (to do that requires more advanced API usage).

这篇关于是否存在生成POM.xml文件的Java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆