use of org.apache.maven.archiver.MavenArchiver in project maven-plugins by apache.
the class WarMojo method performPackaging.
/**
* Generates the webapp according to the <tt>mode</tt> attribute.
*
* @param warFile the target WAR file
* @throws IOException if an error occurred while copying files
* @throws ArchiverException if the archive could not be created
* @throws ManifestException if the manifest could not be created
* @throws DependencyResolutionRequiredException if an error occurred while resolving the dependencies
* @throws MojoExecutionException if the execution failed
* @throws MojoFailureException if a fatal exception occurred
*/
private void performPackaging(File warFile) throws IOException, ManifestException, DependencyResolutionRequiredException, MojoExecutionException, MojoFailureException {
getLog().info("Packaging webapp");
buildExplodedWebapp(getWebappDirectory());
MavenArchiver archiver = new MavenArchiver();
archiver.setArchiver(warArchiver);
archiver.setOutputFile(warFile);
// CHECKSTYLE_OFF: LineLength
getLog().debug("Excluding " + Arrays.asList(getPackagingExcludes()) + " from the generated webapp archive.");
getLog().debug("Including " + Arrays.asList(getPackagingIncludes()) + " in the generated webapp archive.");
// CHECKSTYLE_ON: LineLength
warArchiver.addDirectory(getWebappDirectory(), getPackagingIncludes(), getPackagingExcludes());
final File webXmlFile = new File(getWebappDirectory(), "WEB-INF/web.xml");
if (webXmlFile.exists()) {
warArchiver.setWebxml(webXmlFile);
}
warArchiver.setRecompressAddedZips(isRecompressZippedFiles());
warArchiver.setIncludeEmptyDirs(isIncludeEmptyDirectories());
if (Boolean.FALSE.equals(failOnMissingWebXml) || (failOnMissingWebXml == null && isProjectUsingAtLeastServlet30())) {
getLog().debug("Build won't fail if web.xml file is missing.");
warArchiver.setExpectWebXml(false);
}
// create archive
archiver.createArchive(getSession(), getProject(), getArchive());
// create the classes to be attached if necessary
if (isAttachClasses()) {
if (isArchiveClasses() && getJarArchiver().getDestFile() != null) {
// special handling in case of archived classes: MWAR-240
File targetClassesFile = getTargetClassesFile();
FileUtils.copyFile(getJarArchiver().getDestFile(), targetClassesFile);
projectHelper.attachArtifact(getProject(), "jar", getClassesClassifier(), targetClassesFile);
} else {
ClassesPackager packager = new ClassesPackager();
final File classesDirectory = packager.getClassesDirectory(getWebappDirectory());
if (classesDirectory.exists()) {
getLog().info("Packaging classes");
packager.packageClasses(classesDirectory, getTargetClassesFile(), getJarArchiver(), getSession(), getProject(), getArchive());
projectHelper.attachArtifact(getProject(), "jar", getClassesClassifier(), getTargetClassesFile());
}
}
}
if (this.classifier != null) {
projectHelper.attachArtifact(getProject(), "war", this.classifier, warFile);
} else {
Artifact artifact = getProject().getArtifact();
if (primaryArtifact) {
artifact.setFile(warFile);
} else if (artifact.getFile() == null || artifact.getFile().isDirectory()) {
artifact.setFile(warFile);
}
}
}
use of org.apache.maven.archiver.MavenArchiver in project maven-plugins by apache.
the class ClassesPackager method packageClasses.
/**
* Package the classes
*
* @param classesDirectory the classes directory
* @param targetFile the target file
* @param jarArchiver the jar archiver to use
* @param session the current session
* @param project the related project
* @param archiveConfiguration the archive configuration to use
* @throws MojoExecutionException if an error occurred while creating the archive
*/
public void packageClasses(File classesDirectory, File targetFile, JarArchiver jarArchiver, MavenSession session, MavenProject project, MavenArchiveConfiguration archiveConfiguration) throws MojoExecutionException {
try {
final MavenArchiver archiver = new MavenArchiver();
archiver.setArchiver(jarArchiver);
archiver.setOutputFile(targetFile);
archiver.getArchiver().addDirectory(classesDirectory);
archiver.createArchive(session, project, archiveConfiguration);
} catch (ArchiverException e) {
throw new MojoExecutionException("Could not create classes archive", e);
} catch (ManifestException e) {
throw new MojoExecutionException("Could not create classes archive", e);
} catch (IOException e) {
throw new MojoExecutionException("Could not create classes archive", e);
} catch (DependencyResolutionRequiredException e) {
throw new MojoExecutionException("Could not create classes archive", e);
}
}
use of org.apache.maven.archiver.MavenArchiver in project jangaroo-tools by CoreMedia.
the class PackageMojo method execute.
public void execute() throws MojoExecutionException {
File jarFile = new File(targetDir, finalName + "." + Types.JAVASCRIPT_EXTENSION);
MavenArchiver mavenArchiver = new MavenArchiver();
mavenArchiver.setArchiver(archiver);
mavenArchiver.setOutputFile(jarFile);
try {
if (archive.getManifestFile() == null) {
if (useDefaultManifestFile && defaultManifestFile.exists()) {
getLog().info("Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath());
archive.setManifestFile(defaultManifestFile);
} else if (manifest != null) {
archive.setManifestFile(manifest);
} else {
archive.setManifestFile(createDefaultManifest(project));
}
}
if (outputDirectory.exists()) {
mavenArchiver.getArchiver().addDirectory(outputDirectory);
if (!getModuleJsFile().exists() && new File(outputDirectory, moduleClassesJsFile).exists()) {
createDefaultModuleJsFile();
}
}
String groupId = project.getGroupId();
String artifactId = project.getArtifactId();
mavenArchiver.getArchiver().addFile(project.getFile(), "META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml");
mavenArchiver.createArchive(project, archive);
} catch (Exception e) {
// NOSONAR
throw new MojoExecutionException("Failed to create the javascript archive", e);
}
project.getArtifact().setFile(jarFile);
}
use of org.apache.maven.archiver.MavenArchiver in project karaf by apache.
the class KarMojo method createArchive.
/**
* Generates the configuration archive.
*
* @param bundles
*/
@SuppressWarnings("deprecation")
private File createArchive(List<Artifact> bundles, File featuresFile, String groupId, String artifactId, String version) throws MojoExecutionException {
ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();
File archiveFile = getArchiveFile(outputDirectory, finalName, classifier);
MavenArchiver archiver = new MavenArchiver();
MavenArchiveConfiguration configuration = new MavenArchiveConfiguration();
configuration.addManifestEntries(archive.getManifestEntries());
archiver.setArchiver(jarArchiver);
archiver.setOutputFile(archiveFile);
try {
//TODO should .kar be a bundle?
// archive.addManifestEntry(Constants.BUNDLE_NAME, project.getName());
// archive.addManifestEntry(Constants.BUNDLE_VENDOR, project.getOrganization().getName());
// ArtifactVersion version = project.getArtifact().getSelectedVersion();
// String versionString = "" + version.getMajorVersion() + "." + version.getMinorVersion() + "." + version.getIncrementalVersion();
// if (version.getQualifier() != null) {
// versionString += "." + version.getQualifier();
// }
// archive.addManifestEntry(Constants.BUNDLE_VERSION, versionString);
// archive.addManifestEntry(Constants.BUNDLE_MANIFESTVERSION, "2");
// archive.addManifestEntry(Constants.BUNDLE_DESCRIPTION, project.getDescription());
// // NB, no constant for this one
// archive.addManifestEntry("Bundle-License", ((License) project.getLicenses().get(0)).getUrl());
// archive.addManifestEntry(Constants.BUNDLE_DOCURL, project.getUrl());
// //TODO this might need some help
// archive.addManifestEntry(Constants.BUNDLE_SYMBOLICNAME, project.getArtifactId());
//include the feature.xml
Artifact featureArtifact = factory.createArtifactWithClassifier(groupId, artifactId, version, "xml", KarArtifactInstaller.FEATURE_CLASSIFIER);
jarArchiver.addFile(featuresFile, repositoryPath + layout.pathOf(featureArtifact));
if (featureArtifact.isSnapshot()) {
// the artifact is a snapshot, create the maven-metadata-local.xml
getLog().debug("Feature artifact is a SNAPSHOT, handling the maven-metadata-local.xml");
File metadataTarget = new File(featuresFile.getParentFile(), "maven-metadata-local.xml");
getLog().debug("Looking for " + metadataTarget.getAbsolutePath());
if (!metadataTarget.exists()) {
// the maven-metadata-local.xml doesn't exist, create it
getLog().debug(metadataTarget.getAbsolutePath() + " doesn't exist, create it");
Metadata metadata = new Metadata();
metadata.setGroupId(featureArtifact.getGroupId());
metadata.setArtifactId(featureArtifact.getArtifactId());
metadata.setVersion(featureArtifact.getVersion());
metadata.setModelVersion("1.1.0");
Versioning versioning = new Versioning();
versioning.setLastUpdatedTimestamp(new Date(System.currentTimeMillis()));
Snapshot snapshot = new Snapshot();
snapshot.setLocalCopy(true);
versioning.setSnapshot(snapshot);
SnapshotVersion snapshotVersion = new SnapshotVersion();
snapshotVersion.setClassifier(featureArtifact.getClassifier());
snapshotVersion.setVersion(featureArtifact.getVersion());
snapshotVersion.setExtension(featureArtifact.getType());
snapshotVersion.setUpdated(versioning.getLastUpdated());
versioning.addSnapshotVersion(snapshotVersion);
metadata.setVersioning(versioning);
MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer();
try {
Writer writer = new FileWriter(metadataTarget);
metadataWriter.write(writer, metadata);
} catch (Exception e) {
getLog().warn("Could not create maven-metadata-local.xml", e);
getLog().warn("It means that this SNAPSHOT could be overwritten by an older one present on remote repositories");
}
}
getLog().debug("Adding file " + metadataTarget.getAbsolutePath() + " in the jar path " + repositoryPath + layout.pathOf(featureArtifact).substring(0, layout.pathOf(featureArtifact).lastIndexOf('/')) + "/maven-metadata-local.xml");
jarArchiver.addFile(metadataTarget, repositoryPath + layout.pathOf(featureArtifact).substring(0, layout.pathOf(featureArtifact).lastIndexOf('/')) + "/maven-metadata-local.xml");
}
for (Artifact artifact : bundles) {
artifactResolver.resolve(artifact, remoteRepos, localRepo);
//TODO this may not be reasonable, but... resolved snapshot artifacts have timestamped versions
//which do not work in startup.properties.
artifact.setVersion(artifact.getBaseVersion());
if (artifact.isSnapshot()) {
// the artifact is a snapshot, create the maven-metadata-local.xml
final File metadataTmp = File.createTempFile("maven-metadata-local.xml", ".tmp");
try {
MavenUtil.generateMavenMetadata(artifact, metadataTmp);
} catch (Exception e) {
getLog().warn("Could not create maven-metadata-local.xml", e);
getLog().warn("It means that this SNAPSHOT could be overwritten by an older one present on remote repositories");
}
jarArchiver.addFile(metadataTmp, repositoryPath + layout.pathOf(artifact).substring(0, layout.pathOf(artifact).lastIndexOf('/')) + "/maven-metadata-local.xml");
try {
metadataTmp.delete();
} catch (final Exception ex) {
getLog().warn("Cannot delete temporary created file.", ex);
}
}
String targetFileName = repositoryPath + layout.pathOf(artifact);
jarArchiver.addFile(artifact.getFile(), targetFileName);
}
if (resourcesDir.isDirectory()) {
archiver.getArchiver().addDirectory(resourcesDir);
}
archiver.createArchive(project, archive);
return archiveFile;
} catch (Exception e) {
throw new MojoExecutionException("Failed to create archive", e);
}
}
use of org.apache.maven.archiver.MavenArchiver in project maven-plugins by apache.
the class AbstractJarMojo method createArchive.
/**
* Generates the JAR.
* @return The instance of File for the created archive file.
* @throws MojoExecutionException in case of an error.
*/
public File createArchive() throws MojoExecutionException {
File jarFile = getJarFile(outputDirectory, finalName, getClassifier());
MavenArchiver archiver = new MavenArchiver();
archiver.setArchiver(jarArchiver);
archiver.setOutputFile(jarFile);
archive.setForced(forceCreation);
try {
File contentDirectory = getClassesDirectory();
if (!contentDirectory.exists()) {
getLog().warn("JAR will be empty - no content was marked for inclusion!");
} else {
archiver.getArchiver().addDirectory(contentDirectory, getIncludes(), getExcludes());
}
archiver.createArchive(session, project, archive);
return jarFile;
} catch (Exception e) {
// TODO: improve error handling
throw new MojoExecutionException("Error assembling JAR", e);
}
}
Aggregations