20

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!

0

4 Answers 4

51

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

3
  • 1
    Wow! I mean... that is actually what I was looking for. I really just can do all the setters and the write the pom.xml file anywhere I want. I have tried a little bit this library and seems to do wonders. Really, thanks! :)
    – Random
    Commented Jan 25, 2010 at 15:45
  • 4
    If you are wondering how Brett knows this, it is because he wrote Apache Maven2: Effective Implementation: brettporter.wordpress.com Commented Jan 26, 2010 at 17:15
  • This is awesome. Thanks a lot for the suggestion. This library is quite usefule...!!! Commented Jul 2, 2013 at 9:39
4
        MavenXpp3Reader reader = new MavenXpp3Reader();
        Model pomModel = reader.read(new FileReader(pomLibFile));
        final List<Dependency> dependencies= pomModel.getDependencies();
        final List<String> modules= pomModel.getModules();
        final List<Profile> profiles = pomModel.getProfiles();

        InputStream inputStream = new FileInputStream(new File(pomLibFile));
        StringWriter writer = new StringWriter();
        IOUtils.copy(inputStream, writer, "utf-8");
        pomModel.getDependencyManagement();
        final Properties properties = new Properties();
        properties.load(new FileInputStream(pomProperties));
        RegexBasedInterpolator interpolator = new RegexBasedInterpolator();

        interpolator.addValueSource( new EnvarBasedValueSource() );
        interpolator.addValueSource( new PropertiesBasedValueSource( properties ) );

        List<String> synonymPrefixes = new ArrayList<String>();
        synonymPrefixes.add( "project." );
        synonymPrefixes.add( "pom." );

        PrefixedValueSourceWrapper modelWrapper = new PrefixedValueSourceWrapper( new ObjectBasedValueSource( pomModel ),synonymPrefixes, true );
        interpolator.addValueSource( modelWrapper );

        PrefixedValueSourceWrapper pomPropertyWrapper = 
                new PrefixedValueSourceWrapper( new PropertiesBasedValueSource( pomModel.getProperties() ), synonymPrefixes,  true );
        interpolator.addValueSource( pomPropertyWrapper );

        interpolator.addValueSource( new PropertiesBasedValueSource( properties ) );

        RecursionInterceptor recursionInterceptor = new PrefixAwareRecursionInterceptor( synonymPrefixes, true );

        String serializedPOM = interpolator.interpolate( writer.toString(), recursionInterceptor );
        System.out.println("-------- "+serializedPOM);;

Reference : http://plexus.codehaus.org/plexus-components/plexus-interpolation/index.html

though I am still stuck if I have to add multiple (unknown number of) dependencies.

2

To generate pom with multiple dependencies, you can use the following sample code:

Model model = new Model();  
Writer writer = new FileWriter("C:/GRADLE_WORKSPACE/test.pom");  
List<Dependency> dependencyList = new ArrayList<Dependency>();  

model.setGroupId( "TestGroupArtifactID" );    
model.setArtifactId("TestGroupArtifactName");    
model.setVersion("1.0.0");    

Dependency dep = new Dependency();    
dep.setGroupId("TestGroupId");    
dep.setArtifactId("TestDependencyName");    
dep.setVersion("1.0.0");
dependencyList.add(dep);

Dependency dep2 = new Dependency();    
dep2.setGroupId("TestGroupId2");    
dep2.setArtifactId("TestDependencyName2");   
dep2.setVersion("2.0.0");     
dependencyList.add(dep2);       

//model.addDependency(dep);    
model.setDependencies(dependencyList);    
new MavenXpp3Writer().write(writer, model );    
writer.close();

Regards,
Srikanth Praveen

1

Why do you have to do it in java rather than using an existing tool such as m2eclipse.
See guide for creating a POM for an existing project using m2eclipse.

You could also see the m2eclipse developer guide which will let you see the source code for their implementation.

Reply----
This is a common problem encountered when trying to mavenise a project.
The biggest hurdle is trying to identify the correct maven coordinates.
Often projects refer to renamed jar files, where the group-id, and version numbers have been stripped off.

Sometimes inspecting the manifest in the jar-file gives some hints as to the correct dependent artifact.

2
  • The idea is not to only generate the pom.xml of the project I am working, but to generate a pom.xml file of any project. I understand that they store the data needed and then give it to me to create the pom. I don't know how they store this data (or why they just don't generate a pom instead of storing it) I only know that I am to recive it (hurray modular bussines). Thanks for the guides I would look what I can do with their code :)
    – Random
    Commented Jan 25, 2010 at 10:35
  • Thanks for the piece of advise :) Although I am given this data so I hope that they are correct and real. Anyway, shall I have problems with Maven coordinates then I shall inspect how the data is given and I suposse it would be some type of connection problem (but I hope that doesn't happen!). The answer given by Brett Porter was just what I actually needed. I really hope there are no more problems :)
    – Random
    Commented Jan 26, 2010 at 8:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.