Is there a Java Class that generates a POM.xml file?

后端 未结 4 1824
梦毁少年i
梦毁少年i 2020-12-05 07:54

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

Wha

相关标签:
4条回答
  • 2020-12-05 08:18

    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

    0 讨论(0)
  • 2020-12-05 08:26
            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.

    0 讨论(0)
  • 2020-12-05 08:35

    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.

    0 讨论(0)
  • 2020-12-05 08:44

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

    0 讨论(0)
提交回复
热议问题