Juan Hernandez has uploaded a new change for review. Change subject: core: Maven plugin to generate JBoss modules ......................................................................
core: Maven plugin to generate JBoss modules This change introduces a new maven plugin that generates .zip files containing JBoss modules and attaches them as artifacts. Change-Id: Ie9f3eb59fd67d4ce97f3e2802e7058fa76feb4eb Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com> --- A build-tools-root/jboss-modules-maven-plugin/pom.xml A build-tools-root/jboss-modules-maven-plugin/src/main/java/org/ovirt/engine/build/Module.java A build-tools-root/jboss-modules-maven-plugin/src/main/java/org/ovirt/engine/build/ModulesMojo.java M build-tools-root/pom.xml M pom.xml 5 files changed, 377 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/72/12072/1 diff --git a/build-tools-root/jboss-modules-maven-plugin/pom.xml b/build-tools-root/jboss-modules-maven-plugin/pom.xml new file mode 100644 index 0000000..47969bb --- /dev/null +++ b/build-tools-root/jboss-modules-maven-plugin/pom.xml @@ -0,0 +1,105 @@ +<?xml version="1.0"?> + +<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> + <groupId>org.ovirt.engine</groupId> + <artifactId>build-tools-root</artifactId> + <version>3.3.0-SNAPSHOT</version> + </parent> + + <name>oVirt JBoss Modules Maven Plugin</name> + + <description> + Maven Plugin used to generate and attach an artifact containing + JBoss modules + </description> + + <artifactId>jboss-modules-maven-plugin</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>maven-plugin</packaging> + + <dependencies> + + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-artifact</artifactId> + <version>3.0.4</version> + </dependency> + + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-core</artifactId> + <version>3.0.4</version> + </dependency> + + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <version>3.0.4</version> + </dependency> + + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-project</artifactId> + <version>2.2.1</version> + </dependency> + + <dependency> + <groupId>org.apache.maven.plugin-tools</groupId> + <artifactId>maven-plugin-annotations</artifactId> + <version>3.1</version> + </dependency> + + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-archiver</artifactId> + <version>2.1.1</version> + </dependency> + + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-utils</artifactId> + <version>3.0</version> + </dependency> + + </dependencies> + + <build> + + <plugins> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-plugin-plugin</artifactId> + <version>3.2</version> + <configuration> + <goalPrefix>jboss-modules</goalPrefix> + <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound> + </configuration> + <executions> + <execution> + <id>mojo-descriptor</id> + <goals> + <goal>descriptor</goal> + </goals> + </execution> + <execution> + <id>help-goal</id> + <goals> + <goal>helpmojo</goal> + </goals> + </execution> + </executions> + </plugin> + + </plugins> + + </build> + +</project> diff --git a/build-tools-root/jboss-modules-maven-plugin/src/main/java/org/ovirt/engine/build/Module.java b/build-tools-root/jboss-modules-maven-plugin/src/main/java/org/ovirt/engine/build/Module.java new file mode 100644 index 0000000..4eb0325 --- /dev/null +++ b/build-tools-root/jboss-modules-maven-plugin/src/main/java/org/ovirt/engine/build/Module.java @@ -0,0 +1,65 @@ +package org.ovirt.engine.build; + +import org.apache.maven.artifact.Artifact; + +public class Module { + private String artifactId; + private String groupId; + private String moduleName; + private String moduleSlot; + private String resourcePath; + + public Module() { + // Nothing. + } + + public String getArtifactId() { + return artifactId; + } + + public void setArtifactId(String artifactId) { + this.artifactId = artifactId; + } + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public String getModuleName() { + if (moduleName != null) { + return moduleName; + } + return groupId + "." + artifactId; + } + + public void setModuleName(String moduleName) { + this.moduleName = moduleName; + } + + public String getModuleSlot() { + if (moduleSlot != null) { + return moduleSlot; + } + return "main"; + } + + public void setModuleSlot(String moduleSlot) { + this.moduleSlot = moduleSlot; + } + + public String getResourcePath() { + if (resourcePath != null) { + return resourcePath; + } + return artifactId + ".jar"; + } + + public boolean matches(Artifact artifact) { + return artifact.getArtifactId().equals(artifactId) && artifact.getGroupId().equals(groupId); + } +} + diff --git a/build-tools-root/jboss-modules-maven-plugin/src/main/java/org/ovirt/engine/build/ModulesMojo.java b/build-tools-root/jboss-modules-maven-plugin/src/main/java/org/ovirt/engine/build/ModulesMojo.java new file mode 100644 index 0000000..f1a2285 --- /dev/null +++ b/build-tools-root/jboss-modules-maven-plugin/src/main/java/org/ovirt/engine/build/ModulesMojo.java @@ -0,0 +1,188 @@ +package org.ovirt.engine.build; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectHelper; +import org.codehaus.plexus.archiver.zip.ZipArchiver; +import org.codehaus.plexus.util.FileUtils; + +@Mojo(name = "jboss-modules", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyCollection = ResolutionScope.COMPILE) +public class ModulesMojo extends AbstractMojo { + + /** + * The maven project. + */ + @Component + private MavenProject project; + + /** + * The project helper. + */ + @Component + private MavenProjectHelper projectHelper; + + /** + * The name of the module. + */ + @Parameter(property = "moduleName", required = false) + private String moduleName; + + /** + * The slot of the module. + */ + @Parameter(property = "moduleSlot", required = false, defaultValue = "main") + private String moduleSlot; + + /** + * The list of modules to generate. + */ + @Parameter(property = "modules") + private List<Module> modules; + + /** + * The temporary directory where modules will be stored. + */ + private File modulesDir; + + public void execute() throws MojoExecutionException { + // Make sure the list of modules is not empty: + if (modules == null) { + modules = new ArrayList<Module>(1); + } + for (Module module: modules) { + getLog().info("module: " + module); + } + + // Populate the module map and slot map with the value for the + // artifact of this project: + if (modules.isEmpty()) { + Module module = new Module(); + module.setArtifactId(project.getArtifactId()); + module.setGroupId(project.getGroupId()); + modules.add(module); + } + + // Locate the target directory: + File targetDir = new File(project.getBuild().getDirectory()); + + // Create the modules directory in the temporary build directory: + modulesDir = new File(targetDir, "modules"); + getLog().info("Creating modules directory \"" + modulesDir + "\""); + if (!modulesDir.exists()) { + if (!modulesDir.mkdirs()) { + throw new MojoExecutionException( + "Can't create target modules directory \"" + + modulesDir.getAbsolutePath() + "\""); + } + } + + // Copy any content from the source modules directory to the modules + // directory: + String sourcePath = "src" + File.separator + "main" + File.separator + "modules"; + File sourceDir = new File(project.getBasedir(), sourcePath); + getLog().info("Copying module resources to \"" + modulesDir); + if (sourceDir.exists()) { + try { + FileUtils.copyDirectoryStructure(sourceDir, modulesDir); + } + catch (IOException exception) { + throw new MojoExecutionException( + "Can't copy source modules directory \"" + sourceDir.getAbsolutePath() + "\" " + + "to target modules directory \"" + modulesDir.getAbsolutePath() + "\"", + exception); + } + } + + // Generate the modules: + for (Module module: modules) { + createModule(module); + } + + // Create the archive containing all the contents of the modules + // directory: + File modulesArchive = new File(targetDir, project.getBuild().getFinalName() + "-modules.zip"); + ZipArchiver modulesArchiver = new ZipArchiver(); + modulesArchiver.setDestFile(modulesArchive); + modulesArchiver.addDirectory(modulesDir); + getLog().info("Creating module archive \"" + modulesArchive + "\"."); + try { + modulesArchiver.createArchive(); + } + catch (Exception exception) { + throw new MojoExecutionException( + "Can't generate modules archive \"" + modulesArchive.getAbsolutePath() + "\"", + exception); + } + + // Attach the generated zip file containing the modules as an + // additional artifact: + getLog().info("Attaching modules artifact \"" + modulesArchive + "\""); + projectHelper.attachArtifact(project, "zip", "modules", modulesArchive); + } + + private void createModule(Module module) throws MojoExecutionException { + // Create the slot directory: + String modulePath = module.getModuleName().replace(".", File.separator); + String slotPath = modulePath + File.separator + module.getModuleSlot(); + File slotDir = new File(modulesDir, slotPath); + getLog().info("Creating slot directory \"" + slotDir + "\""); + if (!slotDir.exists()) { + if (!slotDir.mkdirs()) { + throw new MojoExecutionException( + "Can't create module directory \"" + + slotDir.getAbsolutePath() + "\""); + } + } + + // Find the dependency with the same group and artifact id that the module: + Artifact matchingArtifact = null; + if (module.matches(project.getArtifact())) { + matchingArtifact = project.getArtifact(); + } + else { + for (Artifact currentArtifact: project.getDependencyArtifacts()) { + if (module.matches(currentArtifact)) { + matchingArtifact = currentArtifact; + } + } + } + if (matchingArtifact == null) { + throw new MojoExecutionException( + "Can't find dependency matching artifact id \"" + module.getArtifactId() + "\" " + + "and group id \"" + module.getGroupId() + "\""); + } + + // Copy the artifact to the slot directory: + File artifactFrom = matchingArtifact.getFile(); + if (artifactFrom == null) { + throw new MojoExecutionException( + "Can't find file for artifact id \"" + module.getArtifactId() + "\" " + + "and group id \"" + module.getGroupId() + "\""); + } + File artifactTo = new File(slotDir, module.getResourcePath()); + getLog().info("Copying artifact to \"" + artifactTo + "\""); + try { + FileUtils.copyFile(artifactFrom, artifactTo); + } + catch (IOException exception) { + throw new MojoExecutionException( + "Can't copy artifact from \"" + artifactFrom.getAbsolutePath() + "\" " + + "to \"" + artifactTo.getAbsolutePath() + "\"", + exception); + } + } + +} + diff --git a/build-tools-root/pom.xml b/build-tools-root/pom.xml index bf0aea5..ddf163b 100644 --- a/build-tools-root/pom.xml +++ b/build-tools-root/pom.xml @@ -12,6 +12,7 @@ <description>Parent pom of build tools available for use by oVirt modules</description> <modules> <module>checkstyles</module> + <module>jboss-modules-maven-plugin</module> <module>ovirt-checkstyle-extension</module> </modules> </project> diff --git a/pom.xml b/pom.xml index 772ef60..949299c 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,7 @@ <maven-compiler-plugin.version>2.3.2</maven-compiler-plugin.version> <gwt.plugin.version>2.3.0</gwt.plugin.version> <test-jar.plugin.version>2.2</test-jar.plugin.version> + <jboss-modules.plugin.version>1.0-SNAPSHOT</jboss-modules.plugin.version> </properties> <dependencyManagement> <dependencies> @@ -495,6 +496,23 @@ <version>2.2</version> </plugin> + <!-- Make sure that the goal that generates JBoss modules is + automatically executed in the package phase of any project + that uses the plugin: --> + <plugin> + <groupId>org.ovirt.engine</groupId> + <artifactId>jboss-modules-maven-plugin</artifactId> + <version>${jboss-modules.plugin.version}</version> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>jboss-modules</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> </pluginManagement> <plugins> -- To view, visit http://gerrit.ovirt.org/12072 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie9f3eb59fd67d4ce97f3e2802e7058fa76feb4eb Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches