001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.maven2.aar;
020:
021: import org.apache.maven.archiver.MavenArchiveConfiguration;
022: import org.apache.maven.archiver.MavenArchiver;
023: import org.apache.maven.artifact.Artifact;
024: import org.apache.maven.artifact.DependencyResolutionRequiredException;
025: import org.apache.maven.plugin.MojoExecutionException;
026: import org.apache.maven.project.MavenProjectHelper;
027: import org.codehaus.plexus.archiver.ArchiverException;
028: import org.codehaus.plexus.archiver.jar.JarArchiver;
029: import org.codehaus.plexus.archiver.jar.ManifestException;
030:
031: import java.io.File;
032: import java.io.IOException;
033:
034: /**
035: * Build a aar.
036: *
037: * @goal aar
038: * @phase package
039: * @requiresDependencyResolution runtime
040: */
041: public class AarMojo extends AbstractAarMojo {
042: /**
043: * The directory for the generated aar.
044: *
045: * @parameter expression="${project.build.directory}"
046: * @required
047: */
048: private String outputDirectory;
049:
050: /**
051: * The name of the generated aar.
052: *
053: * @parameter expression="${project.build.finalName}"
054: * @required
055: */
056: private String aarName;
057:
058: /**
059: * The Jar archiver.
060: *
061: * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#jar}"
062: * @required
063: */
064: private JarArchiver jarArchiver;
065:
066: /**
067: * The maven archive configuration to use.
068: *
069: * @parameter
070: */
071: private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
072:
073: /**
074: * Classifier to add to the artifact generated. If given, the artifact will be an attachment
075: * instead.
076: *
077: * @parameter
078: */
079: private String classifier;
080:
081: /**
082: * Whether this is the main artifact being built. Set to <code>false</code> if you don't want to
083: * install or deploy it to the local repository instead of the default one in an execution.
084: *
085: * @parameter expression="${primaryArtifact}" default-value="true"
086: */
087: private boolean primaryArtifact;
088:
089: /** @component */
090: private MavenProjectHelper projectHelper;
091:
092: /**
093: * Executes the AarMojo on the current project.
094: *
095: * @throws MojoExecutionException if an error occured while building the webapp
096: */
097: public void execute() throws MojoExecutionException {
098:
099: File aarFile = new File(outputDirectory, aarName + ".aar");
100:
101: try {
102: performPackaging(aarFile);
103: } catch (Exception e) {
104: throw new MojoExecutionException("Error assembling aar", e);
105: }
106: }
107:
108: /**
109: * Generates the aar.
110: *
111: * @param aarFile the target aar file
112: * @throws IOException
113: * @throws ArchiverException
114: * @throws ManifestException
115: * @throws DependencyResolutionRequiredException
116: *
117: */
118: private void performPackaging(File aarFile) throws IOException,
119: ArchiverException, ManifestException,
120: DependencyResolutionRequiredException,
121: MojoExecutionException {
122:
123: buildExplodedAar();
124:
125: // generate aar file
126: getLog().info("Generating aar " + aarFile.getAbsolutePath());
127: MavenArchiver archiver = new MavenArchiver();
128: archiver.setArchiver(jarArchiver);
129: archiver.setOutputFile(aarFile);
130: jarArchiver.addDirectory(aarDirectory);
131:
132: // create archive
133: archiver.createArchive(project, archive);
134:
135: if (classifier != null) {
136: projectHelper.attachArtifact(project, "aar", classifier,
137: aarFile);
138: } else {
139: Artifact artifact = project.getArtifact();
140: if (primaryArtifact) {
141: artifact.setFile(aarFile);
142: } else if (artifact.getFile() == null
143: || artifact.getFile().isDirectory()) {
144: artifact.setFile(aarFile);
145: } else {
146: projectHelper.attachArtifact(project, "aar", aarFile);
147: }
148: }
149: }
150: }
|