如何在运行时编辑 Maven POM?

发布于 2024-09-01 02:35:53 字数 87 浏览 3 评论 0原文

我需要在运行时编辑 POM。我使用 Dom4j 读取 pom,然后设置一些数据。但我需要知道是否存在另一种形式来做到这一点。是否存在一个 Maven 实用程序?

I need editing POM at runtime. I used Dom4j for read pom and after that set some data. But i need know if exist another form for to do this. Exist a maven utilities for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜警司 2024-09-08 02:35:53

使用 MavenXpp3Reader 读取和 MavenXpp3Writer 写入 Model 对象。简单的示例:

String baseDir = "/your/project/basedir/";

//Reading
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileInputStream(new File(baseDir, "/pom.xml")));

//Editing
model.setUrl("http://stackoverflow.com");

//Writing
MavenXpp3Writer writer = new MavenXpp3Writer();
writer.write(new FileOutputStream(new File(baseDir, "/pom.xml")), model);

请注意,任何注释、多余的空格或行都将从文件中删除。

Use MavenXpp3Reader to read and MavenXpp3Writer to write Model objects. Simple example:

String baseDir = "/your/project/basedir/";

//Reading
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileInputStream(new File(baseDir, "/pom.xml")));

//Editing
model.setUrl("http://stackoverflow.com");

//Writing
MavenXpp3Writer writer = new MavenXpp3Writer();
writer.write(new FileOutputStream(new File(baseDir, "/pom.xml")), model);

And notice that any comment, extra white spaces or lines will be removed from the file.

风渺 2024-09-08 02:35:53

根据您要更改的内容,可能会有 Maven 插件。例如maven release 插件会更新pom.xml中的版本信息。 xml 并将更改检查到版本控制中。

尝试搜索您要完成的特定任务(例如“maven 插件版本号更新”),而不是更通用的“修改 pom.xml”。

Depending on what you are changing, there may be maven plugins. For example the maven release plugin updates the version information in the pom.xml and checks the changes into version control.

Try searching for the specific task you are trying to accomplish (e.g. "maven plugin version number update") rather than the more generic "modify pom.xml".

雨落□心尘 2024-09-08 02:35:53

此代码适用于我:

package or.jrichardsz;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;

import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;

public class TestMavenPomEdit {

    public static void main(String[] args) throws Exception {

        //read initial pom
        Model model = parsePomXmlFileToMavenPomModel("C:\\Users\\User\\Desktop\\initial_pom.xml");

        //add some pom modification
        Plugin plugin = new Plugin();
        plugin.setGroupId("com.jelastic");

        model.getBuild().addPlugin(plugin);

        //write new pom
        parseMavenPomModelToXmlString("C:\\Users\\User\\Desktop\\final_pom.xml", model);

    }

    public static Model parsePomXmlFileToMavenPomModel(String path) throws Exception {

        Model model = null;
        FileReader reader = null;
        MavenXpp3Reader mavenreader = new MavenXpp3Reader();
        reader = new FileReader(path);
            model = mavenreader.read(reader);
        return model;

    }   

    public static void parseMavenPomModelToXmlString(String path,Model model) throws Exception {
        MavenXpp3Writer mavenWriter = new MavenXpp3Writer();
        Writer writer = new FileWriter(path);
        mavenWriter.write(writer, model);
    }       

}

TestMavenPomEdit.java

HTH

This code works for me:

package or.jrichardsz;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;

import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;

public class TestMavenPomEdit {

    public static void main(String[] args) throws Exception {

        //read initial pom
        Model model = parsePomXmlFileToMavenPomModel("C:\\Users\\User\\Desktop\\initial_pom.xml");

        //add some pom modification
        Plugin plugin = new Plugin();
        plugin.setGroupId("com.jelastic");

        model.getBuild().addPlugin(plugin);

        //write new pom
        parseMavenPomModelToXmlString("C:\\Users\\User\\Desktop\\final_pom.xml", model);

    }

    public static Model parsePomXmlFileToMavenPomModel(String path) throws Exception {

        Model model = null;
        FileReader reader = null;
        MavenXpp3Reader mavenreader = new MavenXpp3Reader();
        reader = new FileReader(path);
            model = mavenreader.read(reader);
        return model;

    }   

    public static void parseMavenPomModelToXmlString(String path,Model model) throws Exception {
        MavenXpp3Writer mavenWriter = new MavenXpp3Writer();
        Writer writer = new FileWriter(path);
        mavenWriter.write(writer, model);
    }       

}

TestMavenPomEdit.java

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文