简体   繁体   English

是否有生成POM.xml文件的Java类?

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

In my job project I have recently been asked to generate POM files via a java class. 在我的工作项目中,我最近被要求通过java类生成POM文件。 The problem is that I am very, very new to Maven (like since last December). 问题是我对Maven非常非常新(就像去年12月一样)。

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. 我需要的是一个生成xml文件的某种代码(一个pom文件,但是如果我可以配置任何xml创建代码,那将很好)给出所有必要的数据,所以他们不需要手工编写它。 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? 我不知道我是否在解释自己,但问题是,是否有任何库或类生成或构造具有给定数据的POM文件? 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). 如果不是那么好,我只是不想花更多的时间来寻找我不知道的东西,如果它甚至存在,或者是否像宣告POM对象一样简单然后做一个简单的文档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文件有多难,所以我应该有一个简单的方法来做它们但我想我已经迷失了很多API javadoc并且我找不到回来的路。

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. 我的想法是,如果没有代码,那就是搜索POM字典(覆盖所有元素)并使用XML生成器创建一个带有给定POM对象的xml文件(我以前填充了我提供的数据)例如JDOM,XOM或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. PS:我已经读过Eclipse项目正在做一些Maven的事情,并且有一个API为你拥有的实际项目生成一个pom.xml文件。 That would be a great thing if I could override the input data or something. 如果我可以覆盖输入数据或其他东西,这将是一件好事。

Thanks for all! 谢谢大家!

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,最好的方法是通过Maven原型(你可以用你想要的模板创建自己的原型)。

If you really have a need to programmatically write a POM, you can use the following: 如果您确实需要以编程方式编写POM,则可以使用以下命令:

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. ...其中w是java.io.Writer并添加了所有必要的异常处理。

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

To access this API, you should add this dependency: 要访问此API,您应该添加此依赖项:

<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). 还有一个相应的读取API,但请记住,它不会执行所有Maven操作,例如继承和插值(要做到这一点,需要更高级的API使用)。

        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 参考: 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. 虽然如果我必须添加多个(未知数量)依赖项,我仍然会陷入困境。

To generate pom with multiple dependencies, you can use the following sample code: 要生成具有多个依赖项的pom,可以使用以下示例代码:

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

Why do you have to do it in java rather than using an existing tool such as m2eclipse. 为什么必须在java中而不是使用m2eclipse等现有工具。
See guide for creating a POM for an existing project using m2eclipse. 请参阅使用m2eclipse为现有项目创建POM的指南

You could also see the m2eclipse developer guide which will let you see the source code for their implementation. 您还可以看到m2eclipse开发人员指南 ,它将让您查看其实现的源代码。

Reply---- 答复 - -
This is a common problem encountered when trying to mavenise a project. 这是在尝试对项目进行编组时遇到的常见问题。
The biggest hurdle is trying to identify the correct maven coordinates . 最大的障碍是尝试识别正确的maven坐标
Often projects refer to renamed jar files, where the group-id, and version numbers have been stripped off. 项目通常是指重命名的jar文件,其中group-id和版本号已被删除。

Sometimes inspecting the manifest in the jar-file gives some hints as to the correct dependent artifact. 有时检查jar文件中的清单会给出一些关于正确依赖工件的提示。

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

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