Skip to content

Commit cacd9e8

Browse files
committed
Fix eclipse-tycho#602 - Align pom-generation with flatten plugin
Signed-off-by: Christoph Läubrich <[email protected]>
1 parent 4e8adec commit cacd9e8

File tree

5 files changed

+133
-57
lines changed

5 files changed

+133
-57
lines changed

tycho-maven-plugin/src/main/resources/META-INF/plexus/components.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
<test>
3737
org.apache.maven.plugins:maven-surefire-plugin:${surefire-plugin.version}:test
3838
</test>
39+
<prepare-package>
40+
org.eclipse.tycho:tycho-packaging-plugin:${project.version}:update-consumer-pom
41+
</prepare-package>
3942
<package>
4043
org.eclipse.tycho:tycho-packaging-plugin:${project.version}:package-plugin,
4144
org.eclipse.tycho:tycho-p2-plugin:${project.version}:p2-metadata-default
@@ -103,6 +106,9 @@
103106
<process-test-resources>
104107
org.apache.maven.plugins:maven-resources-plugin:${resources-plugin.version}:testResources
105108
</process-test-resources>
109+
<prepare-package>
110+
org.eclipse.tycho:tycho-packaging-plugin:${project.version}:update-consumer-pom
111+
</prepare-package>
106112
<package>
107113
org.eclipse.tycho:tycho-packaging-plugin:${project.version}:package-plugin,
108114
org.eclipse.tycho:tycho-p2-plugin:${project.version}:p2-metadata-default
@@ -161,6 +167,7 @@
161167
<generate-resources>
162168
</generate-resources>
163169
<prepare-package>
170+
org.eclipse.tycho:tycho-packaging-plugin:${project.version}:update-consumer-pom
164171
</prepare-package>
165172
<package>
166173
org.eclipse.tycho:tycho-packaging-plugin:${project.version}:package-feature,
@@ -322,7 +329,7 @@
322329
org.eclipse.tycho:tycho-p2-publisher-plugin:${project.version}:publish-products,
323330
org.eclipse.tycho:tycho-p2-publisher-plugin:${project.version}:publish-categories,
324331
org.eclipse.tycho:tycho-p2-publisher-plugin:${project.version}:attach-artifacts,
325-
org.eclipse.tycho:tycho-p2-repository-plugin:${project.version}:assemble-repository,
332+
org.eclipse.tycho:tycho-p2-repository-plugin:${project.version}:assemble-repository
326333
</prepare-package>
327334
<package>
328335
org.eclipse.tycho:tycho-p2-repository-plugin:${project.version}:archive-repository

tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/AbstractTychoPackagingMojo.java

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,13 @@
1414
package org.eclipse.tycho.packaging;
1515

1616
import java.io.File;
17-
import java.io.IOException;
1817
import java.util.Arrays;
19-
import java.util.Collections;
2018
import java.util.LinkedHashSet;
2119
import java.util.List;
2220
import java.util.Map;
23-
import java.util.Objects;
2421
import java.util.Set;
2522

26-
import org.apache.maven.artifact.Artifact;
2723
import org.apache.maven.execution.MavenSession;
28-
import org.apache.maven.model.Dependency;
29-
import org.apache.maven.model.Model;
30-
import org.apache.maven.model.io.ModelReader;
31-
import org.apache.maven.model.io.ModelWriter;
3224
import org.apache.maven.plugin.AbstractMojo;
3325
import org.apache.maven.plugin.MojoExecutionException;
3426
import org.apache.maven.plugins.annotations.Component;
@@ -90,12 +82,6 @@ public abstract class AbstractTychoPackagingMojo extends AbstractMojo {
9082
@Component
9183
private IncludeValidationHelper includeValidationHelper;
9284

93-
@Component(role = ModelWriter.class)
94-
protected ModelWriter modelWriter;
95-
96-
@Component(role = ModelReader.class)
97-
protected ModelReader modelReader;
98-
9985
/**
10086
* @return a {@link FileSet} with the given includes and excludes and the configured default
10187
* excludes. An empty list of includes leads to an empty file set.
@@ -149,40 +135,4 @@ protected void checkBinIncludesExist(BuildProperties buildProperties, String...
149135
includeValidationHelper.checkBinIncludesExist(project, buildProperties, strictBinIncludes, ignoredIncludes);
150136
}
151137

152-
/**
153-
* Updates the pom file with the dependencies from the model writing it to the
154-
* output directory under [finalName].jar
155-
*
156-
* @param finalName
157-
* @return the maven project with updated pom location
158-
* @throws IOException
159-
*/
160-
protected MavenProject updatePom(String finalName) throws IOException {
161-
getLog().debug("Generate pom descriptor with updated dependencies...");
162-
Model projectModel = modelReader.read(project.getFile(), null);
163-
File pomFile;
164-
if (buildDirectory == null) {
165-
// this should only happen in unit-tests ...
166-
pomFile = new File(project.getBasedir(), finalName + ".pom");
167-
} else {
168-
pomFile = new File(buildDirectory, finalName + ".pom");
169-
}
170-
List<Dependency> dependencies = projectModel.getDependencies();
171-
dependencies.clear();
172-
List<Dependency> list = Objects.requireNonNullElse(project.getDependencies(), Collections.emptyList());
173-
for (Dependency dep : list) {
174-
Dependency copy = dep.clone();
175-
copy.setSystemPath(null);
176-
if (dep.getGroupId().startsWith("p2.eclipse.") && Artifact.SCOPE_SYSTEM.equals(dep.getScope())) {
177-
copy.setOptional(true);
178-
copy.setScope(Artifact.SCOPE_PROVIDED);
179-
}
180-
dependencies.add(copy);
181-
}
182-
modelWriter.write(pomFile, null, projectModel);
183-
MavenProject mavenProject = project.clone(); // don't alter the original project!
184-
mavenProject.setFile(pomFile);
185-
return mavenProject;
186-
}
187-
188138
}

tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/PackageFeatureMojo.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
156156
archive.setAddMavenDescriptor(false);
157157
}
158158
MavenProject mavenProject = project;
159-
if (archive.isAddMavenDescriptor()) {
160-
mavenProject = updatePom(finalName);
161-
}
162159
archiver.createArchive(session, mavenProject, archive);
163160
} catch (Exception e) {
164161
throw new MojoExecutionException("Error creating feature package", e);

tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/PackagePluginMojo.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,6 @@ private File createPluginJar() throws MojoExecutionException {
252252
getLog().warn("ignoring unsupported archive forced = false parameter.");
253253
archive.setForced(true);
254254
}
255-
if (archive.isAddMavenDescriptor()) {
256-
mavenProject = updatePom(finalName);
257-
}
258255
archiver.createArchive(session, mavenProject, archive);
259256
return pluginFile;
260257
} catch (IOException | ArchiverException | ManifestException | DependencyResolutionRequiredException e) {
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)