summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-07-08 09:54:32 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2013-07-08 09:54:32 +0200
commit6a42b734a4120c8a27ebfb87172bb8f020bf00a1 (patch)
tree9c4a2300aba53795b97da74a99df37147a9c8eed
downloadclasspath-maven-plugin-6a42b734a4120c8a27ebfb87172bb8f020bf00a1.tar.gz
classpath-maven-plugin-6a42b734a4120c8a27ebfb87172bb8f020bf00a1.tar.bz2
classpath-maven-plugin-6a42b734a4120c8a27ebfb87172bb8f020bf00a1.tar.xz
classpath-maven-plugin-6a42b734a4120c8a27ebfb87172bb8f020bf00a1.zip
o Initial import of classpath mojo.
-rw-r--r--.gitattributes22
-rwxr-xr-x.gitignore3
-rwxr-xr-xpom.xml67
-rwxr-xr-xsrc/main/java/io/trygvis/maven/classpath/ClasspathMojo.java125
4 files changed, 217 insertions, 0 deletions
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..703390b
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,22 @@
+# Default behaviour, in case users don't have core.autocrlf set
+* text=auto
+
+# Explicitly declare text files we want to always be normalized and
+# converted to native line endings on checkout
+*.java text
+*.properties text
+*.xml text
+*.css text
+*.js text
+*.html text
+
+
+# shell scripts must always use lf
+*.sh text eol=lf
+
+
+# binary files, should never be modified
+*.png binary
+*.jpg binary
+*.pdf binary
+*.jar binary
diff --git a/.gitignore b/.gitignore
new file mode 100755
index 0000000..612c5bc
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+target
+.idea
+*.iml
diff --git a/pom.xml b/pom.xml
new file mode 100755
index 0000000..49e57c2
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,67 @@
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>io.trygvis</groupId>
+ <artifactId>trygvis-parent</artifactId>
+ <version>1</version>
+ </parent>
+ <groupId>io.trygvis.maven</groupId>
+ <artifactId>classpath-maven-plugin</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>maven-plugin</packaging>
+ <properties>
+ <maven.compiler.source>1.5</maven.compiler.source>
+ <maven.compiler.target>1.5</maven.compiler.target>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>3.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-core</artifactId>
+ <version>3.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.plugin-tools</groupId>
+ <artifactId>maven-plugin-annotations</artifactId>
+ <version>3.1</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.sonatype.plexus</groupId>
+ <artifactId>plexus-build-api</artifactId>
+ <version>0.0.7</version>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-plugin-plugin</artifactId>
+ <version>3.2</version>
+ <configuration>
+ <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
+ </configuration>
+ <executions>
+ <execution>
+ <id>mojo-descriptor</id>
+ <goals>
+ <goal>descriptor</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>help-goal</id>
+ <goals>
+ <goal>helpmojo</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
+
diff --git a/src/main/java/io/trygvis/maven/classpath/ClasspathMojo.java b/src/main/java/io/trygvis/maven/classpath/ClasspathMojo.java
new file mode 100755
index 0000000..4cc8d39
--- /dev/null
+++ b/src/main/java/io/trygvis/maven/classpath/ClasspathMojo.java
@@ -0,0 +1,125 @@
+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.
+ * <p>
+ * For example, if the project depends on commons-lang 2.6 and commons-math 2.2,
+ * then the file written will contain the line:
+ * <pre>
+ * commons-lang/commons-lang/2.6/commons-lang-2.6.jar
+ * org/apache/commons/commons-math/2.2/commons-math-2.2.jar
+ * </pre>
+ */
+@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<String> paths = buildClasspath();
+ writeClasspath(paths);
+ addGeneratedResourceToBuild();
+ }
+
+ @SuppressWarnings("unchecked")
+ private List<String> buildClasspath() {
+ ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();
+// List<Artifact> artifacts = project.getRuntimeArtifacts();
+// Collection<Artifact> artifacts = project.getDependencyArtifacts();
+ Collection<Artifact> artifacts = project.getArtifacts();
+ List<String> paths = new ArrayList<String>(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<String> 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<String> 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);
+ }
+}