Skip to content

Commit

Permalink
Bug 572418 - Class files from project.build.testOutputDirectory should
Browse files Browse the repository at this point in the history
be incooperated into a "test" artifact

Change-Id: Id456f54d0449ca80d55285e18ce71ced6bb535ef
Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
  • Loading branch information
laeubi authored and mickaelistria committed Apr 6, 2021
1 parent 7504dc1 commit 9485693
Showing 1 changed file with 82 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
Expand All @@ -31,11 +32,14 @@
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.MavenProjectHelper;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.jar.ManifestException;
import org.codehaus.plexus.archiver.util.DefaultFileSet;
import org.codehaus.plexus.component.annotations.Requirement;
import org.eclipse.tycho.PackagingType;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.core.TychoConstants;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
Expand All @@ -44,6 +48,7 @@
import org.eclipse.tycho.core.shared.BuildProperties;
import org.eclipse.tycho.packaging.sourceref.SourceReferenceComputer;
import org.eclipse.tycho.packaging.sourceref.SourceReferencesProvider;
import org.osgi.framework.Constants;

/**
* Creates a jar-based plugin and attaches it as an artifact
Expand Down Expand Up @@ -148,18 +153,73 @@ public class PackagePluginMojo extends AbstractTychoPackagingMojo {
@Component
private SourceReferenceComputer soureReferenceComputer;

@Requirement
private MavenProjectHelper projectHelper;

@Override
public void execute() throws MojoExecutionException {
synchronized (LOCK) {
pdeProject = (EclipsePluginProject) DefaultReactorProject.adapt(project)
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
pdeProject = (EclipsePluginProject) reactorProject
.getContextValue(TychoConstants.CTX_ECLIPSE_PLUGIN_PROJECT);

createSubJars();

File pluginFile = createPluginJar();

project.getArtifact().setFile(pluginFile);
File testPluginFile = createTestPluginJar(reactorProject);
if (testPluginFile != null) {
projectHelper.attachArtifact(project, "jar", PackagingType.TYPE_ECLIPSE_TEST_PLUGIN, testPluginFile);
}

}
}

private File createTestPluginJar(ReactorProject reactorProject) throws MojoExecutionException {
File testOutputDirectory = reactorProject.getBuildDirectory().getTestOutputDirectory();
if (!testOutputDirectory.isDirectory() || testOutputDirectory.list().length == 0) {
return null;
}

JarArchiver archiver = new JarArchiver();

File fragmentFile = new File(buildDirectory, finalName + "_fragment.jar");
if (fragmentFile.exists()) {
fragmentFile.delete();
}
File manifest = new File(project.getBuild().getDirectory(), "FRAGMENT_MANIFEST.MF");
try {
Manifest bundleManifest = getManifest();
Manifest fragmentManifest = new Manifest();
Attributes attributes = fragmentManifest.getMainAttributes();
attributes.put(Name.MANIFEST_VERSION, "1.0");
attributes.putValue(Constants.BUNDLE_MANIFESTVERSION, "2");
attributes.putValue(Constants.BUNDLE_NAME, "Test Fragment for " + project.getGroupId() + ":"
+ project.getArtifactId() + ":" + project.getVersion());
String hostVersion = bundleManifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
String hostSymbolicName = bundleManifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
attributes.putValue(Constants.BUNDLE_VERSION, hostVersion);
attributes.putValue(Constants.BUNDLE_SYMBOLICNAME, hostSymbolicName + ".test");
attributes.putValue(Constants.FRAGMENT_HOST,
hostSymbolicName + ";" + Constants.BUNDLE_VERSION_ATTRIBUTE + "=\"" + hostVersion + "\"");
Collection<String> additionalBundles = pdeProject.getBuildProperties().getAdditionalBundles();
if (!additionalBundles.isEmpty()) {
attributes.putValue(Constants.REQUIRE_BUNDLE, String.join(",", additionalBundles));
}
attributes.putValue(Constants.DYNAMICIMPORT_PACKAGE, "*");
writeManifest(manifest, fragmentManifest);
} catch (IOException e) {
throw new MojoExecutionException("Update Manifest failed", e);
}
archiver.setManifest(manifest);
archiver.setDestFile(fragmentFile);
archiver.addDirectory(testOutputDirectory);
try {
archiver.createArchive();
} catch (IOException | ArchiverException e) {
throw new MojoExecutionException("Error assembling test JAR", e);
}
return fragmentFile;
}

private void createSubJars() throws MojoExecutionException {
Expand Down Expand Up @@ -229,10 +289,9 @@ private File createPluginJar() throws MojoExecutionException {
checkBinIncludesExist(buildProperties, binIncludesIgnoredForValidation.toArray(new String[0]));
archiver.getArchiver().addFileSet(getFileSet(project.getBasedir(), binIncludesList, binExcludesList));

File manifest = updateManifest();
if (manifest.exists()) {
archive.setManifestFile(manifest);
}
File manifest = new File(project.getBuild().getDirectory(), "MANIFEST.MF");
updateManifest(manifest);
archive.setManifestFile(manifest);

archiver.setOutputFile(pluginFile);
if (!archive.isForced()) {
Expand All @@ -248,10 +307,23 @@ private File createPluginJar() throws MojoExecutionException {
}
}

private File updateManifest() throws IOException, MojoExecutionException {
File mfile = new File(project.getBasedir(), "META-INF/MANIFEST.MF");
private void updateManifest(File output) throws IOException, MojoExecutionException {

writeManifest(output, getManifest());
}

protected void writeManifest(File output, Manifest mf) throws IOException {
File parentFile = output.getParentFile();
if (!parentFile.mkdirs() && !parentFile.exists()) {
throw new IOException("creating target directory " + parentFile.getAbsolutePath() + " failed");
}
try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(output))) {
mf.write(os);
}
}

InputStream is = new FileInputStream(mfile);
protected Manifest getManifest() throws IOException, MojoExecutionException {
InputStream is = new FileInputStream(new File(project.getBasedir(), "META-INF/MANIFEST.MF"));
Manifest mf;
try {
mf = new Manifest(is);
Expand All @@ -267,13 +339,7 @@ private File updateManifest() throws IOException, MojoExecutionException {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
attributes.putValue("Bundle-Version", reactorProject.getExpandedVersion());
soureReferenceComputer.addSourceReferenceHeader(mf, sourceReferences, project);
mfile = new File(project.getBuild().getDirectory(), "MANIFEST.MF");
mfile.getParentFile().mkdirs();
try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(mfile))) {
mf.write(os);
}

return mfile;
return mf;
}

}

0 comments on commit 9485693

Please sign in to comment.