
org.bluestemsoftware.open.eoa.plugin.archive.DeploymentArchiverMojo Maven / Gradle / Ivy
/**
* Copyright 2008 Bluestem Software LLC. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*/
package org.bluestemsoftware.open.eoa.plugin.archive;
/*
* Adapted from org.apache.maven.plugin.jar.JarMojo which was released under the following
* license:
*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.TypeArtifactFilter;
import org.apache.maven.plugin.MojoExecutionException;
import org.bluestemsoftware.open.eoa.plugin.AbstractDeploymentMojo;
import org.bluestemsoftware.open.eoa.plugin.util.DependencyHelper;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.util.FileUtils;
/**
* Packages an EOA deployment artifact. Note that plexus component descriptor included within
* plugin dist binds package phase within default lifecycle to this mojo's package goal.
*
* @goal package
* @phase package
*/
public class DeploymentArchiverMojo extends AbstractDeploymentMojo {
/**
* The location of the manifest file to be used within attached artifact.
*
* @parameter expression="${basedir}/src/main/release/META-INF/MANIFEST.MF"
*/
private File manifestFile;
/**
* Directory that resources are copied to during the build.
*
* @parameter expression="${project.build.directory}/${project.build.finalName}"
* @required
*/
private String workDirectory;
/**
* The maven archiver to use.
*
* @parameter
*/
private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
private File workDir;
/*
* (non-Javadoc)
* @see org.apache.maven.plugin.AbstractMojo#execute()
*/
public void execute() throws MojoExecutionException {
getLog().info(" ======= ExtensionMojo settings =======");
getLog().info("manifestFile[" + manifestFile + "]");
getLog().info("workDirectory[" + workDirectory + "]");
getLog().info("buildDirectory[" + buildDirectory + "]");
getLog().info("finalName[" + finalName + "]");
// compiler and resources mojo dumps everything into the
// target/classes directory. copy the META-INF dir and
// contents as-as to the working dir
try {
File source = new File(buildDirectory, "classes/META-INF/");
if (source.exists()) {
File target = new File(getWorkDir(), "META-INF/");
FileUtils.copyDirectoryStructure(source, target);
}
} catch (IOException e) {
throw new MojoExecutionException("Error copying meta-inf directory", e);
}
// create an EOA-INF directory under working directory
File eoaInfDir = new File(getWorkDir(), "EOA-INF/");
eoaInfDir.mkdirs();
// copy all jar files defined within EOA-INF/lib dir
// to destination with same name
try {
File source = new File(buildDirectory, "classes/EOA-INF/lib/");
if (source.exists()) {
File libDir = new File(eoaInfDir, "lib/");
libDir.mkdirs();
File[] entries = source.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().endsWith(".jar");
}
});
for (File entry : entries) {
FileUtils.copyFileToDirectory(entry, libDir);
}
}
} catch (IOException e) {
throw new MojoExecutionException("Error copying libs", e);
}
// archive contents of EOA-INF/classes dir in source to
// an archive called classes.jar under EOA-INF in dest.
// note that this is necessary, because if run under
// windows, a deployment in exploded format in repository
// could contain paths that exceed 255 char limit
try {
File source = new File(buildDirectory, "classes/EOA-INF/classes/");
if (source.exists()) {
JarArchiver jarArchiver = new JarArchiver();
File classesJar = new File(buildDirectory, "classes.jar");
jarArchiver.setDestFile(classesJar);
jarArchiver.addDirectory(source);
jarArchiver.createArchive();
FileUtils.copyFileToDirectory(classesJar, eoaInfDir);
classesJar.delete();
jarArchiver.reset();
}
} catch (Exception ex) {
throw new MojoExecutionException("Error assembling eoa deployment. ", ex);
}
// copy all 'other' dependency artifacts to root of deployment
// using path as defined by spec. when deployment is deployed
// these dependencies are not resolved from a repository, but
// are instead retrieved from deployment, e.g. *.war
OrArtifactFilter otherFilter = new OrArtifactFilter();
otherFilter.add(new TypeArtifactFilter("eoa-component"));
otherFilter.add(new TypeArtifactFilter("eoa-factory"));
otherFilter.add(new TypeArtifactFilter("eoa-feature"));
Set> explicitArtifacts = DependencyHelper.getProjectDependencies(project, artifactFactory);
Iterator> itr = explicitArtifacts.iterator();
while (itr.hasNext()) {
Artifact artifact = (Artifact)itr.next();
if (!otherFilter.include(artifact) && artifact.getScope().equals("runtime")) {
DependencyHelper.resolveArtifact(artifact, remoteRepositories, artifactResolver, localRepository);
File source = artifact.getFile();
String path = artifact.getGroupId() + "/" + artifact.getArtifactId() + "." + artifact.getType();
File destination = new File(getWorkDir(), path);
try {
FileUtils.copyFile(source, destination);
} catch (IOException ie) {
throw new MojoExecutionException("Error including 'other' dependency", ie);
}
}
}
// create a new archiver otherwise it hangs on to files added
// on previous use and outputs them again to this jar
JarArchiver jarArchiver = new JarArchiver();
File file = null;
try {
file = new File(buildDirectory, finalName + "." + project.getPackaging());
MavenArchiver archiver = new MavenArchiver();
archiver.setArchiver(jarArchiver);
archiver.setOutputFile(file);
includeCustomManifestFile();
archiver.getArchiver().addDirectory(getWorkDir());
archiver.createArchive(project, archive);
project.getArtifact().setFile(file);
} catch (Exception ex) {
throw new MojoExecutionException("Error assembling eoa deployment. ", ex);
}
}
protected File getWorkDir() {
if (workDir == null) {
workDir = new File(workDirectory);
if (!workDir.exists()) {
workDir.mkdir();
}
}
return workDir;
}
private void includeCustomManifestFile() throws IOException {
File customManifestFile = manifestFile;
if (!customManifestFile.exists()) {
getLog().info("Could not find manifest file: " + manifestFile + " - Generating one");
} else {
getLog().info("Including custom manifest file[" + customManifestFile + "]");
archive.setManifestFile(customManifestFile);
File metaInfDir = new File(getWorkDir(), "META-INF");
FileUtils.copyFileToDirectory(customManifestFile, metaInfDir);
}
}
static class OrArtifactFilter implements ArtifactFilter {
private final List filters = new ArrayList();
public boolean include(Artifact artifact) {
boolean include = false;
for (ArtifactFilter filter : filters) {
if (filter.include(artifact)) {
include = true;
break;
}
}
return include;
}
public void add(ArtifactFilter artifactFilter) {
filters.add(artifactFilter);
}
}
}