PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/cdegroot/appassembler
Java | 286 lines | 136 code | 40 blank | 110 comment | 8 complexity | 3daffe71c081385349f2f8cd91566d54 MD5 | raw file
  1. package org.codehaus.mojo.appassembler;
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright 2005-2007 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.io.IOException;
  27. import java.util.Collections;
  28. import java.util.Iterator;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import org.apache.maven.artifact.Artifact;
  32. import org.apache.maven.artifact.factory.ArtifactFactory;
  33. import org.apache.maven.artifact.installer.ArtifactInstallationException;
  34. import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
  35. import org.apache.maven.artifact.repository.ArtifactRepository;
  36. import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
  37. import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
  38. import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
  39. import org.apache.maven.artifact.resolver.ArtifactResolutionException;
  40. import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
  41. import org.apache.maven.artifact.resolver.ArtifactResolver;
  42. import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
  43. import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
  44. import org.apache.maven.artifact.versioning.VersionRange;
  45. import org.apache.maven.plugin.AbstractMojo;
  46. import org.apache.maven.plugin.MojoExecutionException;
  47. import org.apache.maven.plugin.MojoFailureException;
  48. import org.codehaus.plexus.util.FileUtils;
  49. /**
  50. * Creates an appassembler repository. Note that this is deliberately a bit more specific than the assembly plugin
  51. * version - if it can generate a flat layout and exclude JARs, it may be a suitable replacement.
  52. *
  53. * @author <a href="mailto:kristian.nordal@gmail.com">Kristian Nordal</a>
  54. * @version $Id: CreateRepositoryMojo.java 8129 2008-11-22 13:38:18Z kristian $
  55. * @goal create-repository
  56. * @requiresDependencyResolution runtime
  57. * @phase package
  58. */
  59. public class CreateRepositoryMojo
  60. extends AbstractMojo
  61. {
  62. // -----------------------------------------------------------------------
  63. // Parameters
  64. // -----------------------------------------------------------------------
  65. /**
  66. * The directory that will be used to assemble the artifacts in and place the bin scripts.
  67. *
  68. * @required
  69. * @parameter expression="${project.build.directory}/appassembler"
  70. */
  71. private File assembleDirectory;
  72. /**
  73. * The directory that will be used for the dependencies, relative to <code>assembleDirectory</code>.
  74. *
  75. * @required
  76. * @parameter default-value="repo"
  77. * @todo customisation doesn't work due to the shell scripts not honouring it
  78. */
  79. private String repoPath;
  80. /**
  81. * The layout of the generated Maven repository. Supported types - "default" (Maven2) | "legacy" (Maven1) | "flat"
  82. * (flat <code>lib/</code> style).
  83. *
  84. * @parameter default-value="default"
  85. */
  86. private String repositoryLayout;
  87. // -----------------------------------------------------------------------
  88. // Read-only parameters
  89. // -----------------------------------------------------------------------
  90. /**
  91. * @readonly
  92. * @parameter expression="${project.artifacts}"
  93. */
  94. private Set artifacts;
  95. /**
  96. * @readonly
  97. * @parameter expression="${plugin.version}"
  98. */
  99. private String pluginVersion;
  100. /**
  101. * @readonly
  102. * @parameter expression="${localRepository}"
  103. */
  104. private ArtifactRepository localRepository;
  105. /**
  106. * @readonly
  107. * @parameter expression="${project.artifact}"
  108. */
  109. private Artifact projectArtifact;
  110. /**
  111. * Whether to install the booter artifacts into the repository. This may be needed if you are using the Shell script
  112. * generators.
  113. *
  114. * @parameter default-value="false"
  115. */
  116. private boolean installBooterArtifacts;
  117. // -----------------------------------------------------------------------
  118. // Components
  119. // -----------------------------------------------------------------------
  120. /** @component */
  121. private ArtifactFactory artifactFactory;
  122. /**
  123. * @component
  124. */
  125. private ArtifactRepositoryFactory artifactRepositoryFactory;
  126. /**
  127. * @component role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
  128. */
  129. private Map availableRepositoryLayouts;
  130. /** @component */
  131. private ArtifactResolver artifactResolver;
  132. /** @component */
  133. private ArtifactMetadataSource metadataSource;
  134. public void execute()
  135. throws MojoExecutionException, MojoFailureException
  136. {
  137. // ----------------------------------------------------------------------
  138. // Create new repository for dependencies
  139. // ----------------------------------------------------------------------
  140. ArtifactRepositoryLayout artifactRepositoryLayout =
  141. (ArtifactRepositoryLayout) availableRepositoryLayouts.get( repositoryLayout );
  142. if ( artifactRepositoryLayout == null )
  143. {
  144. throw new MojoFailureException( "Unknown repository layout '" + repositoryLayout + "'." );
  145. }
  146. // -----------------------------------------------------------------------
  147. // Initialize
  148. // -----------------------------------------------------------------------
  149. String path = "file://" + assembleDirectory.getAbsolutePath() + "/" + repoPath;
  150. ArtifactRepository artifactRepository =
  151. artifactRepositoryFactory.createDeploymentArtifactRepository( "appassembler", path,
  152. artifactRepositoryLayout, true );
  153. // -----------------------------------------------------------------------
  154. // Install the project's artifact in the new repository
  155. // -----------------------------------------------------------------------
  156. installArtifact( projectArtifact, artifactRepository );
  157. // ----------------------------------------------------------------------
  158. // Install dependencies in the new repository
  159. // ----------------------------------------------------------------------
  160. // TODO: merge with the artifacts below so no duplicate versions included
  161. for ( Iterator it = artifacts.iterator(); it.hasNext(); )
  162. {
  163. Artifact artifact = (Artifact) it.next();
  164. installArtifact( artifact, artifactRepository );
  165. }
  166. if ( installBooterArtifacts )
  167. {
  168. // ----------------------------------------------------------------------
  169. // Install appassembler booter in the new repos
  170. // ----------------------------------------------------------------------
  171. installBooterArtifacts( artifactRepository );
  172. }
  173. }
  174. private void installBooterArtifacts( ArtifactRepository artifactRepository )
  175. throws MojoExecutionException
  176. {
  177. Artifact artifact =
  178. artifactFactory.createDependencyArtifact( "org.codehaus.mojo.appassembler", "appassembler-booter",
  179. VersionRange.createFromVersion( pluginVersion ), "jar", null,
  180. Artifact.SCOPE_RUNTIME );
  181. try
  182. {
  183. Artifact p =
  184. artifactFactory.createBuildArtifact( "org.codehaus.mojo.appassembler", "appassembler-maven-plugin",
  185. pluginVersion, "jar" );
  186. ArtifactFilter filter = new ExcludesArtifactFilter( Collections.singletonList( "junit:junit" ) );
  187. ArtifactResolutionResult result =
  188. artifactResolver.resolveTransitively( Collections.singleton( artifact ), p, localRepository,
  189. Collections.EMPTY_LIST, metadataSource, filter );
  190. for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
  191. {
  192. Artifact a = (Artifact) i.next();
  193. installArtifact( a, artifactRepository );
  194. }
  195. }
  196. catch ( ArtifactResolutionException e )
  197. {
  198. throw new MojoExecutionException( "Failed to copy artifact.", e );
  199. }
  200. catch ( ArtifactNotFoundException e )
  201. {
  202. throw new MojoExecutionException( "Failed to copy artifact.", e );
  203. }
  204. }
  205. private void installArtifact( Artifact artifact, ArtifactRepository artifactRepository )
  206. throws MojoExecutionException
  207. {
  208. if ( artifact.getFile() != null )
  209. {
  210. try
  211. {
  212. // Necessary for the artifact's baseVersion to be set correctly
  213. // See: http://mail-archives.apache.org/mod_mbox/maven-dev/200511.mbox/%3c437288F4.4080003@apache.org%3e
  214. artifact.isSnapshot();
  215. install( artifact.getFile(), artifact, artifactRepository );
  216. }
  217. catch ( ArtifactInstallationException e )
  218. {
  219. throw new MojoExecutionException( "Failed to copy artifact.", e );
  220. }
  221. }
  222. }
  223. public void setAvailableRepositoryLayouts( Map availableRepositoryLayouts )
  224. {
  225. this.availableRepositoryLayouts = availableRepositoryLayouts;
  226. }
  227. public void install( File source, Artifact artifact, ArtifactRepository localRepository )
  228. throws ArtifactInstallationException
  229. {
  230. try
  231. {
  232. String localPath = localRepository.pathOf( artifact );
  233. File destination = new File( localRepository.getBasedir(), localPath );
  234. if ( !destination.getParentFile().exists() )
  235. {
  236. destination.getParentFile().mkdirs();
  237. }
  238. getLog().info( "Installing artifact " + source.getPath() + " to " + destination );
  239. FileUtils.copyFile( source, destination );
  240. }
  241. catch ( IOException e )
  242. {
  243. throw new ArtifactInstallationException( "Error installing artifact: " + e.getMessage(), e );
  244. }
  245. }
  246. }