/appassembler-maven-plugin/src/main/java/org/codehaus/mojo/appassembler/CreateRepositoryMojo.java

https://github.com/mojohaus/appassembler · Java · 204 lines · 98 code · 31 blank · 75 comment · 4 complexity · 675c59dae4e795a741daab3c3f973c31 MD5 · raw file

  1. package org.codehaus.mojo.appassembler;
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright (c) 2006-2012, The Codehaus
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is furnished to do
  12. * so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. import java.io.File;
  26. import java.util.Collections;
  27. import java.util.Iterator;
  28. import java.util.Set;
  29. import org.apache.maven.artifact.Artifact;
  30. import org.apache.maven.artifact.factory.ArtifactFactory;
  31. import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
  32. import org.apache.maven.artifact.repository.ArtifactRepository;
  33. import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
  34. import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
  35. import org.apache.maven.artifact.resolver.ArtifactResolutionException;
  36. import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
  37. import org.apache.maven.artifact.resolver.ArtifactResolver;
  38. import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
  39. import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
  40. import org.apache.maven.artifact.versioning.VersionRange;
  41. import org.apache.maven.plugin.MojoExecutionException;
  42. import org.apache.maven.plugin.MojoFailureException;
  43. import org.apache.maven.plugins.annotations.Component;
  44. import org.apache.maven.plugins.annotations.LifecyclePhase;
  45. import org.apache.maven.plugins.annotations.Mojo;
  46. import org.apache.maven.plugins.annotations.Parameter;
  47. import org.apache.maven.plugins.annotations.ResolutionScope;
  48. /**
  49. * Creates an appassembler repository. Note that this is deliberately a bit more specific than the assembly plugin
  50. * version - if that could generate a flat layout and exclude JARs, it may be a suitable replacement.
  51. *
  52. * @author <a href="mailto:kristian.nordal@gmail.com">Kristian Nordal</a>
  53. */
  54. @Mojo( name = "create-repository", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true )
  55. public class CreateRepositoryMojo
  56. extends AbstractAppAssemblerMojo
  57. {
  58. // -----------------------------------------------------------------------
  59. // Parameters
  60. // -----------------------------------------------------------------------
  61. /**
  62. * The directory that will be used to assemble the artifacts in and place the bin scripts.
  63. */
  64. @Parameter( defaultValue = "${project.build.directory}/appassembler", required = true )
  65. private File assembleDirectory;
  66. /**
  67. * Whether to install the booter artifacts into the repository. This may be needed if you are using the Shell script
  68. * generators.
  69. */
  70. @Parameter( defaultValue = "false" )
  71. private boolean installBooterArtifacts;
  72. /**
  73. * Path (relative to <code>assembleDirectory</code>) of the desired output repository.
  74. *
  75. * @since 1.3.1
  76. * @todo Customization doesn't work due to the shell scripts not honouring it
  77. */
  78. @Parameter( defaultValue = "repo" )
  79. private String repositoryName;
  80. // -----------------------------------------------------------------------
  81. // Read-only parameters
  82. // -----------------------------------------------------------------------
  83. @Parameter( defaultValue = "${project.artifacts}", readonly = true )
  84. private Set<Artifact> artifacts;
  85. @Parameter( defaultValue = "${plugin.version}", readonly = true )
  86. private String pluginVersion;
  87. // -----------------------------------------------------------------------
  88. // Components
  89. // -----------------------------------------------------------------------
  90. @Component
  91. private ArtifactFactory artifactFactory;
  92. @Component
  93. private ArtifactResolver artifactResolver;
  94. @Component
  95. private ArtifactMetadataSource metadataSource;
  96. // -----------------------------------------------------------------------
  97. // AbstractMojo Implementation
  98. // -----------------------------------------------------------------------
  99. /**
  100. * calling from Maven.
  101. *
  102. * @see org.apache.maven.plugin.AbstractMojo#execute()
  103. */
  104. public void execute()
  105. throws MojoExecutionException, MojoFailureException
  106. {
  107. // ----------------------------------------------------------------------
  108. // Create new repository for dependencies
  109. // ----------------------------------------------------------------------
  110. ArtifactRepositoryLayout artifactRepositoryLayout = getArtifactRepositoryLayout();
  111. // -----------------------------------------------------------------------
  112. // Initialize
  113. // -----------------------------------------------------------------------
  114. StringBuilder path = new StringBuilder( "file://" + assembleDirectory.getAbsolutePath() + "/" );
  115. path.append( repositoryName );
  116. ArtifactRepository artifactRepository =
  117. artifactRepositoryFactory.createDeploymentArtifactRepository( "appassembler", path.toString(),
  118. artifactRepositoryLayout, true );
  119. if ( preClean ) {
  120. removeDirectory( new File( artifactRepository.getBasedir() ) );
  121. }
  122. // -----------------------------------------------------------------------
  123. // Install the project's artifact in the new repository
  124. // -----------------------------------------------------------------------
  125. installArtifact( projectArtifact, artifactRepository );
  126. // ----------------------------------------------------------------------
  127. // Install dependencies in the new repository
  128. // ----------------------------------------------------------------------
  129. // TODO: merge with the artifacts below so no duplicate versions included
  130. for ( Artifact artifact : artifacts )
  131. {
  132. installArtifact( artifact, artifactRepository, this.useTimestampInSnapshotFileName );
  133. }
  134. if ( installBooterArtifacts )
  135. {
  136. // ----------------------------------------------------------------------
  137. // Install appassembler booter in the new repos
  138. // ----------------------------------------------------------------------
  139. installBooterArtifacts( artifactRepository );
  140. }
  141. }
  142. private void installBooterArtifacts( ArtifactRepository artifactRepository )
  143. throws MojoExecutionException
  144. {
  145. Artifact artifact =
  146. artifactFactory.createDependencyArtifact( "org.codehaus.mojo.appassembler", "appassembler-booter",
  147. VersionRange.createFromVersion( pluginVersion ), "jar", null,
  148. Artifact.SCOPE_RUNTIME );
  149. try
  150. {
  151. Artifact p =
  152. artifactFactory.createBuildArtifact( "org.codehaus.mojo.appassembler", "appassembler-maven-plugin",
  153. pluginVersion, "jar" );
  154. ArtifactFilter filter = new ExcludesArtifactFilter( Collections.singletonList( "junit:junit" ) );
  155. ArtifactResolutionResult result =
  156. artifactResolver.resolveTransitively( Collections.singleton( artifact ), p, localRepository,
  157. Collections.emptyList(), metadataSource, filter );
  158. for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
  159. {
  160. Artifact a = (Artifact) i.next();
  161. installArtifact( a, artifactRepository, this.useTimestampInSnapshotFileName );
  162. }
  163. }
  164. catch ( ArtifactResolutionException e )
  165. {
  166. throw new MojoExecutionException( "Failed to copy artifact.", e );
  167. }
  168. catch ( ArtifactNotFoundException e )
  169. {
  170. throw new MojoExecutionException( "Failed to copy artifact.", e );
  171. }
  172. }
  173. }