|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2022 Christoph Läubrich and others. |
| 3 | + * This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Christoph Läubrich - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.tycho.packaging; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +import org.apache.maven.artifact.Artifact; |
| 22 | +import org.apache.maven.model.Dependency; |
| 23 | +import org.apache.maven.model.Model; |
| 24 | +import org.apache.maven.model.Parent; |
| 25 | +import org.apache.maven.model.io.ModelReader; |
| 26 | +import org.apache.maven.model.io.ModelWriter; |
| 27 | +import org.apache.maven.plugin.AbstractMojo; |
| 28 | +import org.apache.maven.plugin.MojoExecutionException; |
| 29 | +import org.apache.maven.plugin.MojoFailureException; |
| 30 | +import org.apache.maven.plugins.annotations.Component; |
| 31 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 32 | +import org.apache.maven.plugins.annotations.Mojo; |
| 33 | +import org.apache.maven.plugins.annotations.Parameter; |
| 34 | +import org.apache.maven.plugins.annotations.ResolutionScope; |
| 35 | +import org.apache.maven.project.MavenProject; |
| 36 | + |
| 37 | +/** |
| 38 | + * Updates the pom file with the dependencies from the tycho model. If you |
| 39 | + * further like to customize the pom you should take a look at the <a href= |
| 40 | + * "https://www.mojohaus.org/flatten-maven-plugin/plugin-info.html">Maven |
| 41 | + * Flatten Plugin</a> |
| 42 | + */ |
| 43 | +@Mojo(name = "update-consumer-pom", threadSafe = true, defaultPhase = LifecyclePhase.PREPARE_PACKAGE, requiresDependencyResolution = ResolutionScope.TEST) |
| 44 | +public class UpdateConsumerPomMojo extends AbstractMojo { |
| 45 | + |
| 46 | + private static final String POLYGLOT_POM_TYCHO = ".polyglot.pom.tycho"; |
| 47 | + |
| 48 | + @Parameter(property = "project", readonly = true) |
| 49 | + protected MavenProject project; |
| 50 | + |
| 51 | + @Component(role = ModelWriter.class) |
| 52 | + protected ModelWriter modelWriter; |
| 53 | + |
| 54 | + @Component(role = ModelReader.class) |
| 55 | + protected ModelReader modelReader; |
| 56 | + |
| 57 | + /** |
| 58 | + * The directory where the tycho generated POM file will be written to. |
| 59 | + */ |
| 60 | + @Parameter(defaultValue = "${project.basedir}", required = true) |
| 61 | + protected File outputDirectory; |
| 62 | + |
| 63 | + /** |
| 64 | + * The filename of the tycho generated POM file. |
| 65 | + */ |
| 66 | + @Parameter(defaultValue = ".tycho-pom.xml", required = true) |
| 67 | + protected String tychoPomFilename; |
| 68 | + |
| 69 | + @Parameter(defaultValue = "false") |
| 70 | + protected boolean skipPomGeneration = false; |
| 71 | + |
| 72 | + /** |
| 73 | + * Indicate if the generated tycho POM should become the new project. |
| 74 | + */ |
| 75 | + @Parameter(defaultValue = "true") |
| 76 | + protected boolean updatePomFile = true; |
| 77 | + |
| 78 | + @Override |
| 79 | + public void execute() throws MojoExecutionException, MojoFailureException { |
| 80 | + if (skipPomGeneration) { |
| 81 | + return; |
| 82 | + } |
| 83 | + if (outputDirectory == null) { |
| 84 | + outputDirectory = project.getBasedir(); |
| 85 | + } |
| 86 | + getLog().debug("Generate pom descriptor with updated dependencies..."); |
| 87 | + Model projectModel; |
| 88 | + try { |
| 89 | + projectModel = modelReader.read(project.getFile(), null); |
| 90 | + } catch (IOException e) { |
| 91 | + throw new MojoExecutionException("reading the model failed!", e); |
| 92 | + } |
| 93 | + List<Dependency> dependencies = projectModel.getDependencies(); |
| 94 | + dependencies.clear(); |
| 95 | + List<Dependency> list = Objects.requireNonNullElse(project.getDependencies(), Collections.emptyList()); |
| 96 | + for (Dependency dep : list) { |
| 97 | + Dependency copy = dep.clone(); |
| 98 | + copy.setSystemPath(null); |
| 99 | + if (dep.getGroupId().startsWith("p2.eclipse.") && Artifact.SCOPE_SYSTEM.equals(dep.getScope())) { |
| 100 | + copy.setOptional(true); |
| 101 | + copy.setScope(Artifact.SCOPE_PROVIDED); |
| 102 | + } |
| 103 | + dependencies.add(copy); |
| 104 | + } |
| 105 | + Parent parent = projectModel.getParent(); |
| 106 | + if (parent != null) { |
| 107 | + String relativePath = parent.getRelativePath(); |
| 108 | + if (relativePath != null && relativePath.endsWith(POLYGLOT_POM_TYCHO)) { |
| 109 | + parent.setRelativePath( |
| 110 | + relativePath.substring(0, relativePath.length() - POLYGLOT_POM_TYCHO.length()) + "pom.xml"); |
| 111 | + } |
| 112 | + } |
| 113 | + File output = new File(outputDirectory, tychoPomFilename); |
| 114 | + try { |
| 115 | + modelWriter.write(output, null, projectModel); |
| 116 | + } catch (IOException e) { |
| 117 | + throw new MojoExecutionException("writing the model failed!", e); |
| 118 | + } |
| 119 | + if (updatePomFile) { |
| 120 | + project.setFile(output); |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments