Skip to content

Commit

Permalink
Fix #572 - Insert dynamic dependencies into the jar included pom
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Jan 27, 2022
1 parent 1d5f6da commit 19b91ba
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2011 Sonatype Inc. and others.
* Copyright (c) 2008, 2022 Sonatype Inc. and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -9,17 +9,25 @@
*
* Contributors:
* Sonatype Inc. - initial API and implementation
* Christoph Läubrich - Issue #572 - Insert dynamic dependencies into the jar included pom
*******************************************************************************/
package org.eclipse.tycho.packaging;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.ModelReader;
import org.apache.maven.model.io.ModelWriter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
Expand All @@ -38,6 +46,14 @@

public abstract class AbstractTychoPackagingMojo extends AbstractMojo {

/**
* The output directory of the jar file
*
* By default this is the Maven "target/" directory.
*/
@Parameter(property = "project.build.directory", required = true)
protected File buildDirectory;

@Parameter(property = "session", readonly = true)
protected MavenSession session;

Expand Down Expand Up @@ -73,6 +89,12 @@ public abstract class AbstractTychoPackagingMojo extends AbstractMojo {
@Component
private IncludeValidationHelper includeValidationHelper;

@Component(role = ModelWriter.class)
protected ModelWriter modelWriter;

@Component(role = ModelReader.class)
protected ModelReader modelReader;

/**
* @return a {@link FileSet} with the given includes and excludes and the configured default
* excludes. An empty list of includes leads to an empty file set.
Expand Down Expand Up @@ -126,4 +148,36 @@ protected void checkBinIncludesExist(BuildProperties buildProperties, String...
includeValidationHelper.checkBinIncludesExist(project, buildProperties, strictBinIncludes, ignoredIncludes);
}

/**
* Updates the pom file with the dependencies from the model writing it to the
* output directory under [finalName].jar
*
* @param finalName
* @return the maven project with updated pom location
* @throws IOException
*/
protected MavenProject updatePom(String finalName) throws IOException {
getLog().debug("Generate pom descriptor with updated dependencies...");
Model projectModel = modelReader.read(project.getFile(), null);
File pomFile;
if (buildDirectory == null) {
// this should only happen in unit-tests ...
pomFile = new File(project.getBasedir(), finalName + ".pom");
} else {
pomFile = new File(buildDirectory, finalName + ".pom");
}
List<Dependency> dependencies = projectModel.getDependencies();
dependencies.clear();
List<Dependency> list = Objects.requireNonNullElse(project.getDependencies(), Collections.emptyList());
for (Dependency dep : list) {
Dependency copy = dep.clone();
copy.setSystemPath(null);
dependencies.add(copy);
}
modelWriter.write(pomFile, null, projectModel);
MavenProject mavenProject = project.clone(); // don't alter the original project!
mavenProject.setFile(pomFile);
return mavenProject;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2020 Sonatype Inc. and others.
* Copyright (c) 2008, 2022 Sonatype Inc. and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -10,6 +10,7 @@
* Contributors:
* Sonatype Inc. - initial API and implementation
* Bachmann electronic GmbH. - #519941 Copy the shared license info
* Christoph Läubrich - Issue #572 - Insert dynamic dependencies into the jar included pom
*******************************************************************************/
package org.eclipse.tycho.packaging;

Expand All @@ -32,6 +33,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.FileSet;
import org.codehaus.plexus.archiver.jar.JarArchiver;
Expand Down Expand Up @@ -153,7 +155,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
archive = new MavenArchiveConfiguration();
archive.setAddMavenDescriptor(false);
}
archiver.createArchive(session, project, archive);
MavenProject mavenProject = project;
if (archive.isAddMavenDescriptor()) {
mavenProject = updatePom(finalName);
}
archiver.createArchive(session, mavenProject, archive);
} catch (Exception e) {
throw new MojoExecutionException("Error creating feature package", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2021 Sonatype Inc. and others.
* Copyright (c) 2008, 2022 Sonatype Inc. and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -9,7 +9,8 @@
*
* Contributors:
* Sonatype Inc. - initial API and implementation
* Christoph Läubrich - Automatically translate maven-pom information to osgi Bundle-Header #177
* Christoph Läubrich - Issue #177 - Automatically translate maven-pom information to osgi Bundle-Header
* - Issue #572 - Insert dynamic dependencies into the jar included pom
*******************************************************************************/
package org.eclipse.tycho.packaging;

Expand All @@ -36,6 +37,7 @@
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ArchiverException;
Expand All @@ -59,14 +61,6 @@
@Mojo(name = "package-plugin", threadSafe = true)
public class PackagePluginMojo extends AbstractTychoPackagingMojo {

/**
* The output directory of the jar file
*
* By default this is the Maven "target/" directory.
*/
@Parameter(property = "project.build.directory", required = true)
protected File buildDirectory;

protected EclipsePluginProject pdeProject;

/**
Expand Down Expand Up @@ -244,9 +238,10 @@ private File createPluginJar() throws MojoExecutionException {
}
// 3. handle nested jars and included resources
checkBinIncludesExist(buildProperties, binIncludesIgnoredForValidation.toArray(new String[0]));
archiver.getArchiver().addFileSet(getFileSet(project.getBasedir(), binIncludesList, binExcludesList));
MavenProject mavenProject = project;
archiver.getArchiver().addFileSet(getFileSet(mavenProject.getBasedir(), binIncludesList, binExcludesList));

File manifest = new File(project.getBuild().getDirectory(), "MANIFEST.MF");
File manifest = new File(mavenProject.getBuild().getDirectory(), "MANIFEST.MF");
updateManifest(manifest);
archive.setManifestFile(manifest);

Expand All @@ -257,7 +252,10 @@ private File createPluginJar() throws MojoExecutionException {
getLog().warn("ignoring unsupported archive forced = false parameter.");
archive.setForced(true);
}
archiver.createArchive(session, project, archive);
if (archive.isAddMavenDescriptor()) {
mavenProject = updatePom(finalName);
}
archiver.createArchive(session, mavenProject, archive);
return pluginFile;
} catch (IOException | ArchiverException | ManifestException | DependencyResolutionRequiredException e) {
throw new MojoExecutionException("Error assembling JAR", e);
Expand Down

0 comments on commit 19b91ba

Please sign in to comment.