001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
019: *
020: * --------------------------------------------------------------------------
021: * $Id: WriteFileNameArtifactsMojo.java 1970 2007-10-16 11:49:25Z benoitf $
022: * --------------------------------------------------------------------------
023: */package org.ow2.easybeans.plugin.artifact;
024:
025: import java.io.File;
026: import java.io.FileWriter;
027: import java.io.IOException;
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: import org.apache.maven.artifact.Artifact;
032: import org.apache.maven.artifact.factory.ArtifactFactory;
033: import org.apache.maven.artifact.repository.ArtifactRepository;
034: import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
035: import org.apache.maven.artifact.resolver.ArtifactResolutionException;
036: import org.apache.maven.artifact.resolver.ArtifactResolver;
037: import org.apache.maven.model.Dependency;
038: import org.apache.maven.model.DependencyManagement;
039: import org.apache.maven.plugin.AbstractMojo;
040: import org.apache.maven.plugin.MojoExecutionException;
041: import org.apache.maven.project.MavenProject;
042:
043: /**
044: * Dump the name of the given Artifacts into a file.
045: * @goal resolve
046: * @phase compile
047: * @author Florent Benoit
048: */
049: public class WriteFileNameArtifactsMojo extends AbstractMojo {
050:
051: /**
052: * Artifact Factory used to create Artifacts.
053: * @component
054: */
055: private ArtifactFactory artifactFactory;
056:
057: /**
058: * Artifact Resolver used to resolve artifacts.
059: * @component
060: */
061: private ArtifactResolver artifactResolver;
062:
063: /**
064: * Local Maven repository used by the Artifact Resolver.
065: * @parameter expression="${localRepository}"
066: */
067: private ArtifactRepository localRepository;
068:
069: /**
070: * List of Maven repositories (remote) used by the Artifact Resolver.
071: * @parameter expression="${project.remoteArtifactRepositories}"
072: */
073: private List<?> repositories;
074:
075: /**
076: * Path of the build directory.
077: * @parameter expression="${project.build.directory}"
078: */
079: private File buildDirectory;
080:
081: /**
082: * Name of the file where to print the list of the files.
083: * @parameter
084: */
085: private File outputFile;
086:
087: /**
088: * List of Modules that needs to be printed in a file.
089: * @parameter
090: */
091: private Module[] modules;
092:
093: /**
094: * The maven project.
095: * @parameter expression="${project}"
096: * @required
097: * @readonly
098: */
099: private MavenProject project;
100:
101: /**
102: * Execute the Maven plugin.
103: * @throws MojoExecutionException if the file is not generated.
104: */
105: @SuppressWarnings("unchecked")
106: public void execute() throws MojoExecutionException {
107:
108: // No parameter, use default
109: if (outputFile == null) {
110: outputFile = new File(buildDirectory, "list-of-files.txt");
111: }
112:
113: // Create directory if not existing
114: outputFile.getParentFile().mkdirs();
115:
116: // Build artifacts from modules
117: if (modules == null) {
118: throw new MojoExecutionException(
119: "No modules element has been set");
120: }
121:
122: // Create list
123: List<Artifact> artifacts = new ArrayList<Artifact>();
124:
125: // for each module, build artifact.
126: for (Module module : modules) {
127: String groupId = module.getGroupId();
128: String artifactId = module.getArtifactId();
129: String type = module.getType();
130: String version = module.getVersion();
131:
132: if (groupId == null) {
133: throw new MojoExecutionException(
134: "No groupdId set for module '" + module + "'");
135: }
136: if (artifactId == null) {
137: throw new MojoExecutionException(
138: "No artifactId set for module '" + module + "'");
139: }
140:
141: if (type == null) {
142: getLog().debug(
143: "No type set for module '" + module
144: + "', assume it is jar type");
145: type = "jar";
146: module.setType(type);
147: }
148:
149: if (version == null) {
150: // try to find version with dependency management
151: DependencyManagement dependencyManagement = project
152: .getDependencyManagement();
153:
154: if (dependencyManagement != null) {
155: // try to find a version for this component
156: List<Dependency> dependencies = dependencyManagement
157: .getDependencies();
158: if (dependencies != null) {
159: for (Dependency dependency : dependencies) {
160: getLog()
161: .debug("Dependency = " + dependency);
162: if (groupId.equals(dependency.getGroupId())
163: && artifactId.equals(dependency
164: .getArtifactId())
165: && type
166: .equals(dependency
167: .getType())) {
168: getLog().debug(
169: "Using version of dependency management"
170: + dependency
171: .getVersion());
172: version = dependency.getVersion();
173: break;
174: }
175: }
176: }
177: }
178: if (version == null) {
179: throw new MojoExecutionException(
180: "No version set for module '" + module
181: + "'");
182: }
183: }
184:
185: Artifact artifact = artifactFactory.createBuildArtifact(
186: groupId, artifactId, version, type);
187: getLog().debug("Built Artifact = " + artifact);
188:
189: // now resolve this artifact
190: try {
191: artifactResolver.resolve(artifact, repositories,
192: localRepository);
193: } catch (ArtifactResolutionException e) {
194: throw new MojoExecutionException(
195: "Cannot resolve artifact '" + artifactId + "'.",
196: e);
197: } catch (ArtifactNotFoundException e) {
198: throw new MojoExecutionException("Artifact '"
199: + artifactId + "' not found.", e);
200: }
201:
202: // Add artifact
203: artifacts.add(artifact);
204:
205: }
206:
207: FileWriter fileWriter = null;
208: try {
209: // Create file writer
210: try {
211: fileWriter = new FileWriter(outputFile);
212: } catch (IOException e) {
213: throw new MojoExecutionException(
214: "Cannot create a file on '" + outputFile + "'",
215: e);
216: }
217:
218: // Write the name of the files
219: for (Artifact artifact : artifacts) {
220: try {
221: getLog().debug(
222: "Writing " + artifact.getFile().getName());
223: fileWriter.write(artifact.getFile().getName());
224: fileWriter.write("\n");
225: } catch (IOException e) {
226: throw new MojoExecutionException(
227: "Cannot write the line for Artifact '"
228: + artifact + "' in the file '"
229: + outputFile + "'", e);
230: }
231: }
232: } finally {
233: // close file writer
234: if (fileWriter != null) {
235: try {
236: fileWriter.close();
237: } catch (IOException e) {
238: getLog().debug("Cannot close the file writer", e);
239: }
240: }
241: }
242: }
243: }
|