Skip to content
Browse files

attach-artifact-maven-plugin

git-svn-id: file:///opt/svn/repositories/sonatype.org/plugins/trunk/attach-artifact-maven-plugin@113 c2a14038-686d-4adc-8740-a14e5738cb64
  • Loading branch information...
0 parents commit 6f6d1eadc85738a26b38fd2d5b8d9bd12cd087bc @velo velo committed Jan 9, 2009
43 pom.xml
@@ -0,0 +1,43 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>plugins-parent</artifactId>
+ <groupId>org.sonatype.plugins</groupId>
+ <version>3</version>
+ </parent>
+
+ <artifactId>attach-artifact-maven-plugin</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>maven-plugin</packaging>
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0.9</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
+ <version>2.0.9</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-archiver</artifactId>
+ <version>1.0-alpha-7</version>
+ </dependency>
+ </dependencies>
+</project>
160 src/main/java/org/codehaus/mojo/buildhelper/AttachArtifact.java
@@ -0,0 +1,160 @@
+package org.codehaus.mojo.buildhelper;
+
+/*
+ * The MIT License
+ *
+ * Copyright (c) 2004, The Codehaus
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+import java.io.File;
+
+/**
+ * @author dtran
+ */
+public class AttachArtifact
+{
+ private String artifactId;
+
+ private String classifier;
+
+ private File file;
+
+ private String groupId;
+
+ private File pomFile;
+
+ private String type;
+
+ private Boolean updatePlugin;
+
+ private Boolean updatePom;
+
+ private String version;
+
+ private String goalPrefix;
+
+ public String getGoalPrefix()
+ {
+ return goalPrefix;
+ }
+
+ public void setGoalPrefix( String goalPrefix )
+ {
+ this.goalPrefix = goalPrefix;
+ }
+
+ public String getArtifactId()
+ {
+ return artifactId;
+ }
+
+ public String getClassifier()
+ {
+ return this.classifier;
+ }
+
+ public File getFile()
+ {
+ return this.file;
+ }
+
+ public String getGroupId()
+ {
+ return groupId;
+ }
+
+ public File getPomFile()
+ {
+ return pomFile;
+ }
+
+ public String getType()
+ {
+ return this.type;
+ }
+
+ public String getVersion()
+ {
+ return version;
+ }
+
+ public Boolean isUpdatePlugin()
+ {
+ return updatePlugin;
+ }
+
+ public Boolean isUpdatePom()
+ {
+ return updatePom;
+ }
+
+ public void setArtifactId( String artifactId )
+ {
+ this.artifactId = artifactId;
+ }
+
+ public void setClassifier( String classifier )
+ {
+ this.classifier = classifier;
+ }
+
+ public void setFile( File localFile )
+ {
+ this.file = localFile;
+ }
+
+ public void setGroupId( String groupId )
+ {
+ this.groupId = groupId;
+ }
+
+ public void setPomFile( File pomFile )
+ {
+ this.pomFile = pomFile;
+ }
+
+ public void setType( String type )
+ {
+ this.type = type;
+ }
+
+ public void setUpdatePlugin( Boolean updatePlugin )
+ {
+ this.updatePlugin = updatePlugin;
+ }
+
+ public void setUpdatePom( Boolean updatePom )
+ {
+ this.updatePom = updatePom;
+ }
+
+ public void setVersion( String version )
+ {
+ this.version = version;
+ }
+
+ @Override
+ public String toString()
+ {
+ return ( groupId != null ? groupId + ":" : "" ) + artifactId + ":" + ( version != null ? version + ":" : "" )
+ + type;
+ }
+}
285 src/main/java/org/codehaus/mojo/buildhelper/AttachArtifactMojo.java
@@ -0,0 +1,285 @@
+package org.codehaus.mojo.buildhelper;
+
+/*
+ * The MIT License
+ *
+ * Copyright (c) 2004, The Codehaus
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.archiver.zip.ZipArchiver;
+import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
+import org.codehaus.plexus.util.xml.Xpp3DomWriter;
+
+/**
+ * Attach additional artifacts to be installed and deployed.
+ *
+ * @goal attach-artifact
+ * @phase package
+ * @author <a href="dantran@gmail.com">Dan T. Tran</a>
+ * @author <a href="velo.br@gmail.com">Marvin H. Froeder</a>
+ * @version $Id$
+ * @since 1.0
+ */
+public class AttachArtifactMojo
+ extends AbstractMojo
+{
+ /**
+ * Attach an array of artifacts to the project.
+ *
+ * @parameter
+ * @required
+ */
+ private AttachArtifact[] artifacts;
+
+ /**
+ * @parameter expression="${project}"
+ * @required
+ * @readonly
+ */
+ private MavenProject project;
+
+ /**
+ * @component
+ * @required
+ * @readonly
+ */
+ private ArtifactFactory artifactFactory;
+
+ /**
+ * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="zip"
+ * @required
+ * @readonly
+ */
+ private ZipArchiver zipArchiver;
+
+ /**
+ * @component role="org.codehaus.plexus.archiver.UnArchiver" roleHint="zip"
+ * @required
+ * @readonly
+ */
+ private ZipUnArchiver zipUnArchiver;
+
+ /**
+ * @parameter default-value="${project.build.directory}/attach-artifact-plugin"
+ * @required
+ * @readonly
+ */
+ private File output;
+
+ /**
+ * @parameter
+ */
+ private File defaultPomFile;
+
+ /**
+ * @parameter
+ */
+ private File defaultFile;
+
+ /**
+ * @parameter
+ */
+ private boolean updatePom;
+
+ /**
+ * @parameter
+ */
+ private boolean updatePlugin;
+
+ public void execute()
+ throws MojoExecutionException, MojoFailureException
+ {
+ for ( AttachArtifact attArtifact : artifacts )
+ {
+ try
+ {
+ attachArtifact( attArtifact );
+ }
+ catch ( Exception e )
+ {
+ throw new MojoFailureException( "Error attaching " + attArtifact, e );
+ }
+ }
+ // for ( int i = 0; i < this.artifacts.length; ++i )
+ // {
+ // projectHelper.attachArtifact( this.project, this.artifacts[i].getType(), this.artifacts[i].getClassifier(),
+ // this.artifacts[i].getFile() );
+ // }
+
+ }
+
+ private void attachArtifact( AttachArtifact attArtifact )
+ throws Exception
+ {
+ String groupId = attArtifact.getGroupId() != null ? attArtifact.getGroupId() : project.getGroupId();
+ String artifactId = attArtifact.getArtifactId();
+ String version = attArtifact.getVersion() != null ? attArtifact.getVersion() : project.getVersion();
+ String type = attArtifact.getType() != null ? attArtifact.getType() : "jar";
+ String classifier = attArtifact.getClassifier();
+ File attFile = attArtifact.getFile() != null ? attArtifact.getFile() : defaultFile;
+ File attPomFile = attArtifact.getPomFile() != null ? attArtifact.getPomFile() : defaultPomFile;
+
+ boolean updatePom = attArtifact.isUpdatePom() != null ? attArtifact.isUpdatePom() : this.updatePom;
+ boolean updatePlugin = attArtifact.isUpdatePlugin() != null ? attArtifact.isUpdatePlugin() : this.updatePlugin;
+ Artifact artifact =
+ artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
+ File file;
+ if ( updatePlugin )
+ {
+ String goalPrefix =
+ attArtifact.getGoalPrefix() != null ? attArtifact.getGoalPrefix() : attArtifact.getArtifactId();
+ file = updatePlugin( attFile, groupId, artifactId, version, goalPrefix );
+ }
+ else
+ {
+ file = attFile;
+ }
+ artifact.setFile( file );
+ artifact.setResolved( true );
+
+ project.addAttachedArtifact( artifact );
+
+ if ( attPomFile != null )
+ {
+ Artifact pomArtifact =
+ artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, "pom", classifier );
+
+ File pomFile;
+ if ( updatePom )
+ {
+ pomFile = updatePom( attPomFile, groupId, artifactId, version, type );
+ }
+ else
+ {
+ pomFile = attPomFile;
+ }
+
+ pomArtifact.setFile( pomFile );
+ pomArtifact.setResolved( true );
+
+ project.addAttachedArtifact( pomArtifact );
+ }
+ }
+
+ private File updatePom( File originalPomFile, String groupId, String artifactId, String version, String type )
+ throws Exception
+ {
+ File pomFile = new File( output, artifactId + ".pom" );
+
+ Xpp3Dom dom;
+ FileReader reader = new FileReader( originalPomFile );
+ try
+ {
+ dom = Xpp3DomBuilder.build( reader );
+ Xpp3Dom projectDom = dom;
+ updateValue( projectDom, "groupId", groupId );
+ updateValue( projectDom, "artifactId", artifactId );
+ updateValue( projectDom, "version", version );
+ updateValue( projectDom, "packaging", version );
+
+ }
+ finally
+ {
+ IOUtil.close( reader );
+ }
+
+ FileWriter writer = new FileWriter( pomFile );
+ try
+ {
+ Xpp3DomWriter.write( writer, dom );
+ }
+ finally
+ {
+ IOUtil.close( writer );
+ }
+ return pomFile;
+ }
+
+ private void updateValue( Xpp3Dom dom, String childName, String value )
+ {
+ Xpp3Dom child = dom.getChild( childName );
+ if ( child != null )
+ {
+ child.setValue( value );
+ }
+ }
+
+ private File updatePlugin( File originalFile, String groupId, String artifactId, String version, String goalPrefix )
+ throws Exception
+ {
+ zipArchiver.reset();
+ // zipUnArchiver.reset();
+
+ File output = new File( this.output, artifactId );
+ output.mkdirs();
+
+ zipUnArchiver.setSourceFile( originalFile );
+ zipUnArchiver.setDestDirectory( output );
+ zipUnArchiver.extract();
+
+ File pluginFile = new File( output, "META-INF/maven/plugin.xml" );
+
+ FileReader reader = new FileReader( pluginFile );
+ Xpp3Dom dom;
+ try
+ {
+ dom = Xpp3DomBuilder.build( reader );
+ updateValue( dom, "groupId", groupId );
+ updateValue( dom, "artifactId", artifactId );
+ updateValue( dom, "version", version );
+ updateValue( dom, "goalPrefix", goalPrefix );
+
+ }
+ finally
+ {
+ IOUtil.close( reader );
+ }
+
+ FileWriter writer = new FileWriter( pluginFile );
+ try
+ {
+ Xpp3DomWriter.write( writer, dom );
+ }
+ finally
+ {
+ IOUtil.close( writer );
+ }
+
+ File file = new File( this.output, artifactId + ".jar" );
+ zipArchiver.addDirectory( output );
+ zipArchiver.setDestFile( file );
+ zipArchiver.createArchive();
+ return file;
+ }
+}

0 comments on commit 6f6d1ea

Please sign in to comment.
Something went wrong with that request. Please try again.