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.mar;
020:
021: import java.io.File;
022: import java.io.IOException;
023:
024: import org.apache.maven.archiver.MavenArchiveConfiguration;
025: import org.apache.maven.archiver.MavenArchiver;
026: import org.apache.maven.artifact.Artifact;
027: import org.apache.maven.artifact.DependencyResolutionRequiredException;
028: import org.apache.maven.plugin.MojoExecutionException;
029: import org.apache.maven.project.MavenProjectHelper;
030: import org.codehaus.plexus.archiver.ArchiverException;
031: import org.codehaus.plexus.archiver.jar.JarArchiver;
032: import org.codehaus.plexus.archiver.jar.ManifestException;
033:
034: /**
035: * Build a mar.
036: *
037: * @goal mar
038: * @phase package
039: * @requiresDependencyResolution runtime
040: */
041: public class MarMojo extends AbstractMarMojo {
042: /**
043: * The directory for the generated mar.
044: *
045: * @parameter expression="${project.build.directory}"
046: * @required
047: */
048: private String outputDirectory;
049:
050: /**
051: * The name of the generated mar.
052: *
053: * @parameter expression="${project.build.finalName}"
054: * @required
055: */
056: private String marName;
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 instead.
075: *
076: * @parameter
077: */
078: private String classifier;
079:
080: /**
081: * Whether this is the main artifact being built. Set to <code>false</code> if you don't want to install or deploy
082: * it to the local repository instead of the default one in an execution.
083: *
084: * @parameter expression="${primaryArtifact}" default-value="true"
085: */
086: private boolean primaryArtifact;
087:
088: /**
089: * @component
090: */
091: private MavenProjectHelper projectHelper;
092:
093: /**
094: * Executes the MarMojo on the current project.
095: *
096: * @throws MojoExecutionException
097: * if an error occured while building the webapp
098: */
099: public void execute() throws MojoExecutionException {
100:
101: File marFile = new File(outputDirectory, marName + ".mar");
102:
103: try {
104: performPackaging(marFile);
105: } catch (Exception e) {
106: throw new MojoExecutionException("Error assembling mar", e);
107: }
108: }
109:
110: /**
111: * Generates the mar.
112: *
113: * @param marFile
114: * the target mar file
115: * @throws IOException
116: * @throws ArchiverException
117: * @throws ManifestException
118: * @throws DependencyResolutionRequiredException
119: */
120: private void performPackaging(File marFile) throws IOException,
121: ArchiverException, ManifestException,
122: DependencyResolutionRequiredException,
123: MojoExecutionException {
124:
125: buildExplodedMar();
126:
127: // generate mar file
128: getLog().info("Generating mar " + marFile.getAbsolutePath());
129: MavenArchiver archiver = new MavenArchiver();
130: archiver.setArchiver(jarArchiver);
131: archiver.setOutputFile(marFile);
132: jarArchiver.addDirectory(marDirectory);
133:
134: // create archive
135: archiver.createArchive(project, archive);
136:
137: if (classifier != null) {
138: projectHelper.attachArtifact(project, "mar", classifier,
139: marFile);
140: } else {
141: Artifact artifact = project.getArtifact();
142: if (primaryArtifact) {
143: artifact.setFile(marFile);
144: } else if (artifact.getFile() == null
145: || artifact.getFile().isDirectory()) {
146: artifact.setFile(marFile);
147: } else {
148: projectHelper.attachArtifact(project, "mar", marFile);
149: }
150: }
151: }
152: }
|