Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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.

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

Thanks for all!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
684 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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

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

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

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>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...