From 904829d8a3406655b7654058618c5432a35465ed Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 12 Jul 2013 08:52:53 +0200 Subject: o Stripped down version. --- pom.xml | 4 +- .../io/trygvis/maven/classpath/ClasspathMojo.java | 125 --------------------- .../maven/classpath/ExportClasspathMojo.java | 70 ++++++++++++ .../io/trygvis/maven/classpath/TextFormat.java | 33 ++++++ 4 files changed, 104 insertions(+), 128 deletions(-) delete mode 100755 src/main/java/io/trygvis/maven/classpath/ClasspathMojo.java create mode 100755 src/main/java/io/trygvis/maven/classpath/ExportClasspathMojo.java create mode 100755 src/main/java/io/trygvis/maven/classpath/TextFormat.java diff --git a/pom.xml b/pom.xml index 49e57c2..8868c44 100755 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ io.trygvis trygvis-parent - 1 + 2-SNAPSHOT io.trygvis.maven classpath-maven-plugin @@ -42,7 +42,6 @@ org.apache.maven.plugins maven-plugin-plugin - 3.2 true @@ -64,4 +63,3 @@ - diff --git a/src/main/java/io/trygvis/maven/classpath/ClasspathMojo.java b/src/main/java/io/trygvis/maven/classpath/ClasspathMojo.java deleted file mode 100755 index 4cc8d39..0000000 --- a/src/main/java/io/trygvis/maven/classpath/ClasspathMojo.java +++ /dev/null @@ -1,125 +0,0 @@ -package io.trygvis.maven.classpath; - -import static java.util.Collections.singletonList; -import static java.util.Collections.sort; -import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_RESOURCES; -import static org.apache.maven.plugins.annotations.ResolutionScope.RUNTIME; - -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; -import org.apache.maven.model.Resource; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.Component; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.IOUtil; -import org.sonatype.plexus.build.incremental.BuildContext; - -/** - * Writes the path of each compile/runtime dependency of a project to a file, - * where each path is relative to the root of a maven repository. - *

- * For example, if the project depends on commons-lang 2.6 and commons-math 2.2, - * then the file written will contain the line: - *

- * commons-lang/commons-lang/2.6/commons-lang-2.6.jar
- * org/apache/commons/commons-math/2.2/commons-math-2.2.jar
- * 
- */ -@Mojo(name = "classpath", - defaultPhase = GENERATE_RESOURCES, - requiresDependencyCollection = RUNTIME) -public class ClasspathMojo extends AbstractMojo { - - @Component - private BuildContext context; - - @Parameter(property = "project", required = true, readonly = true) - private MavenProject project; - - @Parameter(defaultValue = "${project.build.directory}/generated-resources/classpath", property = "classpathOutputDirectory") - private File outputDirectory; - - @Parameter(defaultValue = "classpath.txt") - private String file; - - @Parameter(property = "sort", defaultValue = "false") - private boolean sort; - - public void execute() throws MojoExecutionException, MojoFailureException { - List paths = buildClasspath(); - writeClasspath(paths); - addGeneratedResourceToBuild(); - } - - @SuppressWarnings("unchecked") - private List buildClasspath() { - ArtifactRepositoryLayout layout = new DefaultRepositoryLayout(); -// List artifacts = project.getRuntimeArtifacts(); -// Collection artifacts = project.getDependencyArtifacts(); - Collection artifacts = project.getArtifacts(); - List paths = new ArrayList(artifacts.size()); - for (Artifact artifact : artifacts) { -// System.out.println("artifact.getId() = " + artifact.getId()); - String key = artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getClassifier() + ":" + artifact.getVersion() + ":" + artifact.getType() + ":" + artifact.getClassifier(); -// System.out.println("key: " + key); - paths.add(key); - } - - if (sort) { - sort(paths); - } - return paths; - } - - private void writeClasspath(List paths) - throws MojoFailureException, MojoExecutionException { - File output = new File(outputDirectory, file); - File directory = output.getParentFile(); - if (!directory.exists() && ! directory.mkdirs()) { - throw new MojoFailureException("Could not create directory: " + directory); - } - - try { - writeClasspathFile(output, paths); - } catch (IOException ex) { - throw new MojoExecutionException("Could not write to " + file, ex); - } - } - - - private void writeClasspathFile(File output, List classpath) - throws IOException { - OutputStream os = context.newFileOutputStream(output); - PrintWriter writer = null; - try { - writer = new PrintWriter(new OutputStreamWriter(os, "UTF-8")); - for (String s : classpath) { - writer.println(s); - } - } finally { - IOUtil.close(writer); - os.close(); - } - } - - private void addGeneratedResourceToBuild() { - Resource resource = new Resource(); - resource.setDirectory(outputDirectory.getPath()); - resource.setIncludes(singletonList(file)); - project.addResource(resource); - } -} diff --git a/src/main/java/io/trygvis/maven/classpath/ExportClasspathMojo.java b/src/main/java/io/trygvis/maven/classpath/ExportClasspathMojo.java new file mode 100755 index 0000000..de961af --- /dev/null +++ b/src/main/java/io/trygvis/maven/classpath/ExportClasspathMojo.java @@ -0,0 +1,70 @@ +package io.trygvis.maven.classpath; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.IOUtil; +import org.sonatype.plexus.build.incremental.BuildContext; + +import static java.util.Collections.sort; +import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_RESOURCES; +import static org.apache.maven.plugins.annotations.ResolutionScope.RUNTIME; +import static org.codehaus.plexus.util.FileUtils.forceMkdir; + +/** + */ +@Mojo(name = "export-classpath", defaultPhase = GENERATE_RESOURCES, requiresDependencyCollection = RUNTIME) +public class ExportClasspathMojo extends AbstractMojo { + + @Component + private BuildContext context; + + @Parameter(property = "project", required = true, readonly = true) + private MavenProject project; + + @Parameter(defaultValue = "${basedir}", property = "classpath.outputDirectory") + private File outputDirectory; + + @Parameter(defaultValue = "classpath.txt", property = "classpath.file") + private String file; + + @Parameter(property = "sort", defaultValue = "true") + private boolean sort; + + public void execute() throws MojoExecutionException, MojoFailureException { + Artifact artifact = project.getArtifact(); + List artifacts = new ArrayList(project.getArtifacts()); + + if (sort) { + sort(artifacts); + } + + write(artifact, artifacts); + } + + private void write(Artifact artifact, List artifacts) throws MojoExecutionException { + OutputStream stream = null; + try { + forceMkdir(outputDirectory); + stream = context.newFileOutputStream(new File(outputDirectory, file)); + OutputStreamWriter writer = new OutputStreamWriter(stream, "UTF-8"); + TextFormat.write(artifact, artifacts, writer); + } catch (IOException e) { + throw new MojoExecutionException("Error while writing file.", e); + } finally { + IOUtil.close(stream); + } + } +} diff --git a/src/main/java/io/trygvis/maven/classpath/TextFormat.java b/src/main/java/io/trygvis/maven/classpath/TextFormat.java new file mode 100755 index 0000000..cd10a4a --- /dev/null +++ b/src/main/java/io/trygvis/maven/classpath/TextFormat.java @@ -0,0 +1,33 @@ +package io.trygvis.maven.classpath; + +import java.io.PrintWriter; +import java.io.Writer; +import java.util.Collection; + +import org.apache.maven.artifact.Artifact; +import org.codehaus.plexus.util.StringUtils; + +public class TextFormat { + public static void write(Artifact artifact, Collection artifacts, Writer w) { + write(0, artifact, artifacts, w); + } + + private static void write(int indent, Artifact artifact, Collection artifacts, Writer w) { + PrintWriter writer = new PrintWriter(w); + String value = getKey(artifact); + + writer.print(StringUtils.repeat(" ", indent)); + writer.println(value); + + for (Artifact a : artifacts) { + writer.print(StringUtils.repeat(" ", indent)); + writer.println(getKey(a)); + } + + writer.flush(); + } + + private static String getKey(Artifact artifact) { + return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() + ":" + artifact.getType(); + } +} -- cgit v1.2.3