PageRenderTime 22ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/amps-maven-plugin/src/main/java/com/atlassian/maven/plugins/amps/osgi/GenerateObrArtifactMojo.java

https://bitbucket.org/atlassian/amps
Java | 189 lines | 110 code | 27 blank | 52 comment | 8 complexity | 9562f0b593a7bf06b6ae49069670a9e2 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. package com.atlassian.maven.plugins.amps.osgi;
  2. import com.atlassian.maven.plugins.amps.AbstractAmpsMojo;
  3. import org.apache.maven.archiver.MavenArchiveConfiguration;
  4. import org.apache.maven.archiver.MavenArchiver;
  5. import org.apache.maven.artifact.Artifact;
  6. import org.apache.maven.artifact.DependencyResolutionRequiredException;
  7. import org.apache.maven.model.Build;
  8. import org.apache.maven.plugin.MojoExecutionException;
  9. import org.apache.maven.plugin.MojoFailureException;
  10. import org.apache.maven.plugins.annotations.Component;
  11. import org.apache.maven.plugins.annotations.Mojo;
  12. import org.apache.maven.plugins.annotations.Parameter;
  13. import org.apache.maven.project.MavenProject;
  14. import org.apache.maven.project.MavenProjectHelper;
  15. import org.codehaus.plexus.archiver.Archiver;
  16. import org.codehaus.plexus.archiver.ArchiverException;
  17. import org.codehaus.plexus.archiver.jar.JarArchiver;
  18. import org.codehaus.plexus.archiver.jar.ManifestException;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import static java.util.stream.Collectors.toList;
  26. import static org.apache.commons.io.FileUtils.copyFile;
  27. import static org.apache.commons.io.FileUtils.copyFileToDirectory;
  28. /**
  29. * Generates the obr artifact, containing the plugin, its dependencies, and the obr XML file. The OBR file looks like
  30. * this:
  31. * <p/>
  32. * <pre>
  33. * this-plugin.jar
  34. * obr.xml
  35. * dependencies/required-plugin.jar
  36. * </pre>
  37. * <p/>
  38. * All plugins in the root directory will be installed, while the ones in the "dependencies" directory will be installed
  39. * only if they are needed.
  40. */
  41. @Mojo(name = "generate-obr-artifact")
  42. public class GenerateObrArtifactMojo extends AbstractAmpsMojo {
  43. @Parameter
  44. private List<PluginDependency> pluginDependencies = new ArrayList<>();
  45. /**
  46. * The Jar archiver.
  47. */
  48. @Component(role = Archiver.class, hint = "jar")
  49. private JarArchiver jarArchiver;
  50. /**
  51. * The archive configuration to use. See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven
  52. * Archiver Reference</a>.
  53. */
  54. @Parameter
  55. private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
  56. /**
  57. * Specifies whether or not to attach the artifact to the project
  58. */
  59. @Parameter(property = "attach", defaultValue = "true")
  60. private boolean attach;
  61. @Component
  62. private MavenProjectHelper projectHelper;
  63. /**
  64. * The directory where the generated archive file will be put.
  65. */
  66. @Parameter(property = "project.build.directory")
  67. protected File outputDirectory;
  68. /**
  69. * The filename to be used for the generated archive file. The "-obr" suffix will be appended.
  70. */
  71. @Parameter(property = "project.build.finalName")
  72. protected String finalName;
  73. /**
  74. * Contains the full list of projects in the reactor.
  75. */
  76. @Parameter(property = "reactorProjects", readonly = true)
  77. protected List<MavenProject> reactorProjects;
  78. @Parameter
  79. private Map instructions = new HashMap();
  80. public void execute() throws MojoExecutionException, MojoFailureException {
  81. try {
  82. if (!instructions.isEmpty()) {
  83. Build build = getMavenContext().getProject().getBuild();
  84. List<File> deps = resolvePluginDependencies();
  85. getLog().info("the file name is: " + this.finalName);
  86. File obrDir = layoutObr(deps, new File(build.getDirectory(), finalName + ".jar"));
  87. generateObrZip(obrDir);
  88. } else {
  89. getLog().info("Skipping OBR generation... no OSGi bundle manifest instructions found in pom.xml");
  90. }
  91. } catch (IOException e) {
  92. throw new MojoExecutionException(e.getMessage(), e);
  93. }
  94. }
  95. /**
  96. * @param obrDir Directory containing the files to go into the obr zip
  97. * @throws MojoExecutionException If something goes wrong
  98. */
  99. private void generateObrZip(File obrDir) throws MojoExecutionException {
  100. MavenArchiver archiver = new MavenArchiver();
  101. archiver.setArchiver(jarArchiver);
  102. File outputFile = new File(outputDirectory, finalName + ".obr");
  103. final MavenProject mavenProject = getMavenContext().getProject();
  104. try {
  105. archiver.getArchiver().addDirectory(obrDir, "");
  106. archiver.setOutputFile(outputFile);
  107. archive.setAddMavenDescriptor(false);
  108. // todo: be smarter about when this is updated
  109. archive.setForced(true);
  110. archiver.createArchive(mavenProject, archive);
  111. } catch (IOException | ArchiverException | DependencyResolutionRequiredException | ManifestException e) {
  112. throw new MojoExecutionException("Error creating obr archive: " + e.getMessage(), e);
  113. }
  114. if (attach) {
  115. projectHelper.attachArtifact(mavenProject, "obr", outputFile);
  116. } else {
  117. getLog().info("NOT adding OBR to attached artifacts list, so it won't be installed or deployed.");
  118. }
  119. }
  120. /**
  121. * Creates a directory containing the files that will be in the obr artifact.
  122. *
  123. * @param deps The dependencies for this artifact
  124. * @param mainArtifact The main artifact file
  125. * @return The directory containing the future obr zip contents
  126. * @throws IOException If the files cannot be copied
  127. * @throws MojoExecutionException If the dependencies cannot be retrieved
  128. */
  129. private File layoutObr(final List<File> deps, final File mainArtifact) throws MojoExecutionException, IOException {
  130. // create directories
  131. final File obrDir = new File(getMavenContext().getProject().getBuild().getDirectory(), "obr");
  132. obrDir.mkdir();
  133. final File depDir = new File(obrDir, "dependencies");
  134. depDir.mkdir();
  135. // Copy in the dependency plugins for the obr generation
  136. for (File dep : deps) {
  137. copyFileToDirectory(dep, depDir, true);
  138. }
  139. // Generate the obr xml
  140. final File obrXml = new File(obrDir, "obr.xml");
  141. final File[] depFiles = depDir.listFiles();
  142. if (depFiles != null) {
  143. for (File dep : depFiles) {
  144. getMavenGoals().generateObrXml(dep, obrXml);
  145. }
  146. }
  147. // Copy the main artifact over
  148. final File mainArtifactCopy = new File(obrDir, mainArtifact.getName());
  149. copyFile(mainArtifact, mainArtifactCopy);
  150. // Generate the obr xml for the main artifact
  151. // The File must be the one copied into the obrDir (see AMPS-300)
  152. getMavenGoals().generateObrXml(mainArtifactCopy, obrXml);
  153. return obrDir;
  154. }
  155. private List<File> resolvePluginDependencies() {
  156. return getMavenContext().getProject().getDependencyArtifacts().stream()
  157. .filter(artifact -> pluginDependencies.contains(
  158. new PluginDependency(artifact.getGroupId(), artifact.getArtifactId())))
  159. .map(Artifact::getFile)
  160. .collect(toList());
  161. }
  162. }