diff options
Diffstat (limited to 'src/main/java/io/trygvis/maven/classpath/ExportClasspathMojo.java')
-rwxr-xr-x | src/main/java/io/trygvis/maven/classpath/ExportClasspathMojo.java | 70 |
1 files changed, 70 insertions, 0 deletions
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<Artifact> artifacts = new ArrayList<Artifact>(project.getArtifacts()); + + if (sort) { + sort(artifacts); + } + + write(artifact, artifacts); + } + + private void write(Artifact artifact, List<Artifact> 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); + } + } +} |