gpt4 book ai didi

maven-2 - 将父 POM 折叠到子中

转载 作者:行者123 更新时间:2023-12-04 14:53:11 31 4
gpt4 key购买 nike

给定一个带有父 POM 的 maven 项目 - 主要包括插件和依赖版本 - 我如何生成一个 POM,它从父级获取信息,将其放入子级并删除对父级的引用?

理想情况下,这将使用 maven-assembly-plugin 完成。

更新 : 我需要自动完成,因为手动完成很无聊和乏味。

更新 2 : 我正在为外部准备源代码,并且只想交付一个项目,而不是整个家庭。

最佳答案

不久前我遇到了类似的问题,您可以通过定义一个新的 Maven 插件来执行以下操作来避免 Maven 替换属性和解析路径等。

  • 使用标准工件工厂
  • 解决每个父级
  • 使用 MavenXpp3Reader
  • 读取每个 pom 文件(无需解析)
  • 合并 Unresolved 项目
  • 将合并后的项目写入文件

  • 这是我用来为自己证明该过程的一些测试代码,您显然需要将其包装在一个 Maven 插件中并将其绑定(bind)到您的过程的某个阶段。解析后的 pom 默认输出到名称为resolved-pom.xml 的输出目录(即目标),这两个属性可以通过设置“outputDir”和/或“pomfileName”属性用通常的Maven插件配置方法覆盖.
    package name.seller.rich;

    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Writer;
    import java.util.Stack;

    import org.apache.maven.artifact.Artifact;
    import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
    import org.apache.maven.artifact.resolver.ArtifactResolutionException;
    import org.apache.maven.model.Model;
    import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
    import org.apache.maven.plugin.AbstractMojo;
    import org.apache.maven.plugin.MojoExecutionException;
    import org.apache.maven.plugin.MojoFailureException;
    import org.apache.maven.project.MavenProject;
    import org.apache.maven.project.inheritance.ModelInheritanceAssembler;
    import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

    /**
    * @goal output-project
    * @phase process-resources
    * @requiresProject true
    */
    public class OutputResolvedProjectMojo extends AbstractMojo {

    /**
    * Used to look up overlay the parent models on the project's model.
    *
    * @parameter expression=
    * "${component.org.apache.maven.project.inheritance.ModelInheritanceAssembler}"
    * @required
    * @readonly
    */
    private ModelInheritanceAssembler modelInheritanceAssembler;

    /**
    * Used to look up Artifacts in the remote repository.
    *
    * @parameter expression=
    * "${component.org.apache.maven.artifact.factory.ArtifactFactory}"
    * @required
    * @readonly
    */
    protected org.apache.maven.artifact.factory.ArtifactFactory factory;

    /**
    * Used to look up Artifacts in the remote repository.
    *
    * @parameter expression=
    * "${component.org.apache.maven.artifact.resolver.ArtifactResolver}"
    * @required
    * @readonly
    */
    protected org.apache.maven.artifact.resolver.ArtifactResolver artifactResolver;

    /**
    * List of Remote Repositories used by the resolver
    *
    * @parameter expression="${project.remoteArtifactRepositories}"
    * @readonly
    * @required
    */
    protected java.util.List remoteRepos;

    /**
    * Location of the local repository.
    *
    * @parameter expression="${localRepository}"
    * @readonly
    * @required
    */
    protected org.apache.maven.artifact.repository.ArtifactRepository local;

    /**
    * @parameter expression="${project}"
    * @required
    * @readonly
    */
    private MavenProject mavenProject;

    /**
    * The directory to output the resolved project to.
    *
    * @parameter expression="${project.build.directory}"
    */
    private File outputDir;

    /**
    * The directory to output the resolved project to.
    *
    * @parameter expression="resolved-pom.xml"
    */
    private String pomfileName;

    public void execute() throws MojoExecutionException, MojoFailureException {
    MavenProject parentProject = mavenProject.getParent();

    // get the unresolved project by reading the file
    MavenProject bareProject = readBareProject(mavenProject.getFile());

    Stack hierarchy = new Stack();
    hierarchy.push(bareProject);

    try {
    while (parentProject != null) {

    try {
    // get Maven to resolve the parent artifact (download if
    // needed)
    Artifact pomArtifact = this.factory.createArtifact(
    parentProject.getGroupId(), parentProject
    .getArtifactId(), parentProject
    .getVersion(), "", "pom");

    artifactResolver.resolve(pomArtifact, this.remoteRepos,
    this.local);

    // get the file from the local repository and read the bare
    // project
    File parentPomFile = pomArtifact.getFile();

    parentProject = readBareProject(parentPomFile);

    hierarchy.push(parentProject);

    parentProject = parentProject.getParent();
    } catch (ArtifactResolutionException e) {
    getLog().error("can't resolve parent pom", e);
    } catch (ArtifactNotFoundException e) {
    getLog().error("can't resolve parent pom", e);
    }
    }

    // merge each model starting with the oldest ancestors
    MavenProject currentParent = (MavenProject) hierarchy.pop();
    MavenProject currentProject = null;
    while (hierarchy.size() != 0) {
    currentProject = (MavenProject) hierarchy.pop();
    modelInheritanceAssembler.assembleModelInheritance(
    currentProject.getModel(), currentParent.getModel());
    currentParent = currentProject;
    }

    // spit the merged model to the output file.
    Writer writer = getWriter(outputDir, pomfileName);

    if (writer != null) {
    currentProject.writeModel(writer);
    writer.close();
    }
    } catch (IOException e) {
    getLog().error("can't write resolved pom", e);
    }

    }

    /**
    * Creates and returns a writer for outputting the project to a pom file.
    *
    * @param logDir
    * the directory to output the file to.
    * @param logFileName
    * name of the log file
    * @return the writer.
    * @throws IOException
    * if the writer cannot be created.
    */
    private Writer getWriter(final File logDir, final String logFileName)
    throws IOException {
    if (!logDir.exists()) {
    logDir.mkdirs();
    }

    File pomLog = new File(logDir, logFileName);

    if (!pomLog.exists()) {
    pomLog.createNewFile();
    }

    return new FileWriter(pomLog);
    }

    /**
    * Read the mavenProject without resolving any inherited settings.
    *
    * @return the MavenProject for the project's POM
    * @throws MojoExecutionException
    * if the POM can't be parsed.
    */
    MavenProject readBareProject(final File file) {
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model = null;
    try {
    model = reader.read(new FileReader(file));
    } catch (IOException e) {
    getLog().error("can't read pom file", e);
    } catch (XmlPullParserException e) {
    getLog().error("can't read pom file", e);
    }

    return new MavenProject(model);
    }
    }

    关于maven-2 - 将父 POM 折叠到子中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/951643/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com