From 77a0c2d3b73c2bbf33c433baa62f5b21526b121f Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Wed, 2 Nov 2005 02:51:20 +0000 Subject: [PATCH] move to a new package git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@330173 13f79535-47bb-0310-9956-ffa450edef68 --- .../plugins/mavenone/MavenOnePluginMojo.java | 162 ++++++++++++++++++ .../MavenOneRepositoryDeployMojo.java | 133 ++++++++++++++ .../MavenOneRepositoryInstallMojo.java | 155 +++++++++++++++++ 3 files changed, 450 insertions(+) create mode 100644 sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOnePluginMojo.java create mode 100644 sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOneRepositoryDeployMojo.java create mode 100644 sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOneRepositoryInstallMojo.java diff --git a/sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOnePluginMojo.java b/sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOnePluginMojo.java new file mode 100644 index 0000000000..d0531e3260 --- /dev/null +++ b/sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOnePluginMojo.java @@ -0,0 +1,162 @@ +package org.apache.maven.plugins.mavenone; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.archiver.MavenArchiveConfiguration; +import org.apache.maven.archiver.MavenArchiver; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectHelper; +import org.codehaus.plexus.archiver.ArchiverException; +import org.codehaus.plexus.archiver.jar.JarArchiver; +import org.codehaus.plexus.util.FileUtils; + +import java.io.File; + +/** + * Create a Maven 1.x plugin. + * + * @goal maven-one-plugin + * @phase package + */ +public class MavenOnePluginMojo + extends AbstractMojo +{ + + private static final String[] DEFAULT_EXCLUDES = new String[]{"**/package.html"}; + + private static final String[] DEFAULT_INCLUDES = new String[]{"**/**"}; + + /** + * Directory containing the generated JAR. + * + * @parameter expression="${project.build.directory}" + * @required + * @readonly + */ + private File basedir; + + /** + * Name of the generated JAR. + * + * @parameter expression="${project.build.finalName}" + * @required + */ + private String finalName; + + /** + * The Jar archiver. + * + * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#jar}" + * @required + */ + private JarArchiver jarArchiver; + + /** + * The maven project. + * + * @parameter expression="${project}" + * @required + * @readonly + */ + private MavenProject project; + + /** + * @component + */ + private MavenProjectHelper projectHelper; + + /** + * @parameter expression="${project.build.outputDirectory}" + * @required + */ + private File contentDirectory; + + /** + * Generates the JAR. + * + * @todo Add license files in META-INF directory. + */ + public File createArchive() + throws MojoExecutionException + { + File jarFile = new File( basedir, finalName + ".jar" ); + + MavenArchiver archiver = new MavenArchiver(); + + archiver.setArchiver( jarArchiver ); + + archiver.setOutputFile( jarFile ); + + try + { + if ( contentDirectory.exists() ) + { + archiver.getArchiver().addDirectory( contentDirectory, DEFAULT_INCLUDES, DEFAULT_EXCLUDES ); + } + + addFile( archiver, new File( "plugin.jelly" ) ); + addFile( archiver, new File( "plugin.properties" ) ); + addFile( archiver, new File( "project.properties" ) ); + addFile( archiver, new File( "build.properties" ) ); + addFile( archiver, new File( "project.xml" ) ); + addDirectory( archiver, new File( "src/plugin-resources" ) ); + + archiver.createArchive( project, new MavenArchiveConfiguration() ); + + return jarFile; + } + catch ( Exception e ) + { + // TODO: improve error handling + throw new MojoExecutionException( "Error assembling JAR", e ); + } + } + + private static void addDirectory( MavenArchiver archiver, File file ) + throws ArchiverException + { + if ( file.exists() ) + { + archiver.getArchiver().addDirectory( file, file.getName() + "/", DEFAULT_INCLUDES, + FileUtils.getDefaultExcludes() ); + } + } + + private static void addFile( MavenArchiver archiver, File file ) + throws ArchiverException + { + if ( file.exists() ) + { + archiver.getArchiver().addFile( file, file.getName() ); + } + } + + /** + * Generates the JAR. + * + * @todo Add license files in META-INF directory. + */ + public void execute() + throws MojoExecutionException + { + File jarFile = createArchive(); + + project.getArtifact().setFile( jarFile ); + } +} diff --git a/sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOneRepositoryDeployMojo.java b/sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOneRepositoryDeployMojo.java new file mode 100644 index 0000000000..1423e49ca3 --- /dev/null +++ b/sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOneRepositoryDeployMojo.java @@ -0,0 +1,133 @@ +package org.apache.maven.plugins.mavenone; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.deployer.ArtifactDeployer; +import org.apache.maven.artifact.deployer.ArtifactDeploymentException; +import org.apache.maven.artifact.installer.ArtifactInstallationException; +import org.apache.maven.artifact.installer.ArtifactInstaller; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.codehaus.plexus.util.IOUtil; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Properties; + +/** + * Install the artifact in a maven one local repository + * + * @goal deploy-maven-one-repository + * @phase deploy + */ +public class MavenOneRepositoryDeployMojo + extends AbstractMojo +{ + /** + * @parameter expression="${project.packaging}" + * @required + * @readonly + */ + protected String packaging; + + /** + * @parameter expression="${project.file}" + * @required + * @readonly + */ + private File pomFile; + + /** + * @parameter expression="${project.artifact}" + * @required + * @readonly + */ + private Artifact artifact; + + /** + * @component + */ + protected ArtifactDeployer deployer; + + /** + * @component + */ + protected ArtifactRepositoryFactory factory; + + /** + * @parameter expression="${remoteRepositoryId}" default-value="mavenOneRemoteRepository" + * @required + */ + protected String remoteRepositoryId; + + /** + * @parameter expression="${remoteRepositoryUrl}" + * @required + */ + protected String remoteRepositoryUrl; + + /** + * @component roleHint="legacy" + */ + private ArtifactRepositoryLayout legacyLayout; + + /** + * @parameter expression="${localRepository}" + * @required + * @readonly + */ + private ArtifactRepository localRepository; + + public void execute() + throws MojoExecutionException + { + try + { + ArtifactRepository deploymentRepository = factory.createDeploymentArtifactRepository( remoteRepositoryId, + remoteRepositoryUrl, + legacyLayout, false ); + + boolean isPomArtifact = "pom".equals( packaging ); + + if ( isPomArtifact ) + { + deployer.deploy( pomFile, artifact, deploymentRepository, localRepository ); + } + else + { + File file = artifact.getFile(); + if ( file == null ) + { + throw new MojoExecutionException( + "The packaging for this project did not assign a file to the build artifact" ); + } + deployer.deploy( file, artifact, deploymentRepository, localRepository ); + } + + } + catch ( ArtifactDeploymentException e ) + { + throw new MojoExecutionException( e.getMessage(), e ); + } + } +} diff --git a/sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOneRepositoryInstallMojo.java b/sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOneRepositoryInstallMojo.java new file mode 100644 index 0000000000..248c11c96b --- /dev/null +++ b/sandbox/maven-one-plugin/src/main/java/org/apache/maven/plugins/mavenone/MavenOneRepositoryInstallMojo.java @@ -0,0 +1,155 @@ +package org.apache.maven.plugins.mavenone; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.installer.ArtifactInstallationException; +import org.apache.maven.artifact.installer.ArtifactInstaller; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.codehaus.plexus.util.IOUtil; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Properties; + +/** + * Install the artifact in a maven one local repository + * + * @goal install-maven-one-repository + * @phase install + */ +public class MavenOneRepositoryInstallMojo + extends AbstractMojo +{ + /** + * @parameter expression="${project.packaging}" + * @required + * @readonly + */ + protected String packaging; + + /** + * @parameter expression="${project.file}" + * @required + * @readonly + */ + private File pomFile; + + /** + * @parameter expression="${project.artifact}" + * @required + * @readonly + */ + private Artifact artifact; + + /** + * @component + */ + protected ArtifactInstaller installer; + + /** + * @component + */ + protected ArtifactRepositoryFactory factory; + + /** + * @parameter expression="${mavenOneRepository}" + */ + protected String mavenOneRepository; + + /** + * @component roleHint="legacy" + */ + private ArtifactRepositoryLayout legacyLayout; + + public void execute() + throws MojoExecutionException + { + try + { + if ( mavenOneRepository == null ) + { + File f = new File( System.getProperty( "user.home" ), "build.properties" ); + if ( f.exists() ) + { + Properties p = new Properties(); + FileInputStream inStream = new FileInputStream( f ); + try + { + p.load( inStream ); + } + finally + { + IOUtil.close( inStream ); + } + mavenOneRepository = p.getProperty( "maven.repo.local" ); + } + + if ( mavenOneRepository == null ) + { + mavenOneRepository = System.getProperty( "user.home" ) + "/.maven/repository"; + } + } + + File f = new File( mavenOneRepository ); + if ( !f.exists() ) + { + f.mkdirs(); + } + + ArtifactRepository localRepository = factory.createDeploymentArtifactRepository( "mavenOneRepository", + f.toURL().toString(), + legacyLayout, false ); + + boolean isPomArtifact = "pom".equals( packaging ); + + if ( isPomArtifact ) + { + installer.install( pomFile, artifact, localRepository ); + } + else + { + File file = artifact.getFile(); + if ( file == null ) + { + throw new MojoExecutionException( + "The packaging for this project did not assign a file to the build artifact" ); + } + installer.install( file, artifact, localRepository ); + } + + } + catch ( ArtifactInstallationException e ) + { + throw new MojoExecutionException( e.getMessage(), e ); + } + catch ( FileNotFoundException e ) + { + throw new MojoExecutionException( e.getMessage(), e ); + } + catch ( IOException e ) + { + throw new MojoExecutionException( e.getMessage(), e ); + } + } +}