by ampatspell
in Code
While writing a GWT & AppEngine plugin for Maven I came across the Maven concept of “attached” artifacts. While project in Maven can have one “main” artifact, it also can have unlimited count of “attached” artifacts like sources or javadoc. For GWT compile mojo I wanted to attach gwt artifact what would be jar with compiled GWT module(s). This artifact then could be dependency for war project (module) and unpacked in appropriate folder in package phase.
So, to create a jar and attach it as a attached artifact to maven project:
public class DirectoryArtifactService { private final Log log; private final ArchiverManager archiverManager; private final MavenProjectHelper mavenProjectHelper; private final MavenProject mavenProject; public DirectoryArtifactService(Log log, ArchiverManager archiverManager, MavenProjectHelper mavenProjectHelper, MavenProject mavenProject) { this.log = log; this.archiverManager = archiverManager; this.mavenProjectHelper = mavenProjectHelper; this.mavenProject = mavenProject; } public File attachDirectoryArchive(String finalName, String classifier, File outputDirectory, File moduleDirectory) throws MojoExecutionException { String type = "jar"; File output = new File(outputDirectory, format("%s-%s.%s", finalName, classifier, type)); createArchive(output, moduleDirectory); attachArchive(output, type, classifier); return output; } private void createArchive(File output, File directory) throws MojoExecutionException { try { log.info("Creating archive " + output + " from " + directory); Archiver archiver = archiverManager.getArchiver(output); archiver.setDestFile(output); archiver.addDirectory(directory); archiver.createArchive(); } catch (NoSuchArchiverException e) { throw new MojoExecutionException("Failed to create archive " + output, e); } catch (ArchiverException e) { throw new MojoExecutionException("Failed to create archive " + output, e); } catch (IOException e) { throw new MojoExecutionException("Failed to create archive " + output, e); } } private void attachArchive(File output, String type, String classifier) { mavenProjectHelper.attachArtifact(mavenProject, type, classifier, output); } }
Where ArchiverManager is plexus-archiver:
<dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-archiver</artifactId> <version>1.0-alpha-9</version> </dependency>
And all dependencies are injected from plexus container in mojo:
/** * @component */ public ArchiverManager archiverManager; /** * @component */ public MavenProjectHelper mavenProjectHelper; /** * @parameter default-value="${project}" */ public MavenProject mavenProject; public DirectoryArtifactService getDirectoryArtifactService() { return new DirectoryArtifactService(getLog(), archiverManager, mavenProjectHelper, mavenProject); }
After running mojo we can see that Maven also installs gwt artifact:
[INFO] [install:install {execution: default-install}]
[INFO] Installing app/app-gwt/target/app-gwt-1.0-SNAPSHOT.jar to ↩
~/.m2/repository/com/app/app-gwt/1.0-SNAPSHOT/app-gwt-1.0-SNAPSHOT.jar
[INFO] Installing app/app-gwt/target/app-gwt-1.0-SNAPSHOT-gwt.jar to ↩
~/.m2/repository/com/app/app-gwt/1.0-SNAPSHOT/app-gwt-1.0-SNAPSHOT-gwt.jar
[INFO] Installing app/app-gwt/target/app-gwt-1.0-SNAPSHOT-sources.jar to ↩
~/.m2/repository/com/app/app-gwt/1.0-SNAPSHOT/app-gwt-1.0-SNAPSHOT-sources.jar