Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-51214] - Allow invoking Custom WAR Packager Maven Plugin in the CLI Mode #26

Merged
merged 4 commits into from May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions custom-war-packager-maven-plugin/pom.xml
Expand Up @@ -68,7 +68,15 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5</version>
<version>3.5.1</version>
<executions>
<execution>
<id>generated-helpmojo</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand All @@ -78,7 +86,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5</version>
<version>3.5.1</version>
</plugin>
</plugins>
</reporting>
Expand Down
@@ -0,0 +1,134 @@
package io.jenkins.tools.warpackager.mavenplugin;

import io.jenkins.tools.warpackager.lib.config.BuildSettings;
import io.jenkins.tools.warpackager.lib.config.Config;
import io.jenkins.tools.warpackager.lib.impl.Builder;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;

import static org.apache.maven.plugins.annotations.LifecyclePhase.PACKAGE;
import static org.apache.maven.plugins.annotations.ResolutionScope.RUNTIME;

//TODO: There is a serious correlation with Maven HPI's Plugin "custom-war" step. This step is actually used inside via Maven-in-Maven. May be reworked later.

/**
* Mojo wrapper for {@link Builder}.
* @author Oleg Nenashev
* @since TODO
*/
@Mojo(name="build", defaultPhase = PACKAGE, requiresProject = false)
public class BuildMojo extends AbstractMojo {

/**
* Path to the YAML configuration file.
* See the format specification in <a href="https://github.com/jenkinsci/custom-war-packager/">Custom WAR Packager documentation</>
*/
@Parameter(property = "configFile", required = true)
public @CheckForNull String configFilePath;

/**
* Version of the WAR file to be set.
*/
@Parameter(property = "version", defaultValue = BuildSettings.DEFAULT_VERSION)
public @CheckForNull String warVersion;

/**
* Temporary directory, which will be used to build subcomponents and to generate outputs.
*/
@Parameter(property = "tmpDir", defaultValue = BuildSettings.DEFAULT_TMP_DIR_NAME)
public @CheckForNull String tmpDir;

/**
* Optional path to the Maven settings file to be used during the build.
*/
@Parameter(property = "mvnSettingsFile")
public @CheckForNull String mvnSettingsFile;

//TODO: link the format once it's approved as JEP draft
/**
* Optional BOM file, which can be used as an input source.
*/
@Parameter(property = "bomFile")
public @CheckForNull String bom;

/**
* Environment, for which the WAR is being generated.
* If BOM file is defined, Custom WAR packager will use this environment setting to filter components and plugins.
*/
@Parameter(property = "environment")
public @CheckForNull String environment;

/**
* Enforces Batch mode for underlying Maven commands and sets appropriate flags.
* It is recommended to use {@link #mvnSettingsFile} instead to set values.
*/
@Parameter(property = "batchMode")
public boolean batchMode;

/**
* If {@code true}, Custom WAR packager will automatically install the generated artifacts.
*/
@Parameter(property = "installArtifacts")
public boolean installArtifacts;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Config cfg = getConfigOrFail();
build(cfg, new File("tmp"));
}

protected void build(@Nonnull Config cfg, @Nonnull File buildDir) throws MojoExecutionException {
cfg.buildSettings.setVersion(warVersion);
cfg.buildSettings.setInstallArtifacts(installArtifacts);
if (mvnSettingsFile != null) {
cfg.buildSettings.setMvnSettingsFile(new File(mvnSettingsFile));
}
if (tmpDir != null) {
cfg.buildSettings.setTmpDir(new File(tmpDir));
} else { // Use a Maven temporary dir
//TODO: use step ID
cfg.buildSettings.setTmpDir(buildDir);
}

if (batchMode) {
cfg.buildSettings.addMavenOption("--batch-mode");
cfg.buildSettings.addMavenOption("-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird. Delete the option IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it weird?
It may be a bit hackish, but it works if a user does not pass proper settings.xml

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the calling script should be responsible for including -B among general Maven options. As to the log option, I consider this a workaround pending JENKINS-51182.


// BOM
cfg.buildSettings.setEnvironmentName(environment);
if (bom != null) {
cfg.buildSettings.setBOM(new File(bom));
}

final Builder bldr = new Builder(cfg);
try {
bldr.build();
} catch (Exception ex) {
throw new MojoExecutionException("Failed to build the custom WAR", ex);
}
}

protected Config getConfigOrFail() throws MojoExecutionException {
if (configFilePath == null) {
throw new MojoExecutionException("Config file is not defined");
}

final Config cfg;
try {
cfg = Config.loadConfig(new File(configFilePath));
} catch (IOException ex) {
throw new MojoExecutionException("Cannot load configuration from " + configFilePath, ex);
}
return cfg;
}

}
@@ -1,20 +1,14 @@
package io.jenkins.tools.warpackager.mavenplugin;

import io.jenkins.tools.warpackager.lib.config.Config;
import io.jenkins.tools.warpackager.lib.impl.Builder;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;

import javax.annotation.CheckForNull;

import java.io.File;
import java.io.IOException;

import static org.apache.maven.plugins.annotations.LifecyclePhase.*;
import static org.apache.maven.plugins.annotations.ResolutionScope.*;
Expand All @@ -26,28 +20,7 @@
* @since TODO
*/
@Mojo(name="custom-war", defaultPhase = PACKAGE, requiresDependencyResolution = RUNTIME)
public class PackageMojo extends AbstractMojo {

@Parameter
public @CheckForNull String configFilePath;

@Parameter
public @CheckForNull String warVersion;

@Parameter
public @CheckForNull String tmpDir;

@Parameter
public @CheckForNull String mvnSettingsFile;

@Parameter
public @CheckForNull String bom;

@Parameter
public @CheckForNull String environment;

@Parameter
public boolean batchMode;
public class PackageMojo extends BuildMojo {

@Component
protected MavenProject project;
Expand All @@ -57,48 +30,12 @@ public class PackageMojo extends AbstractMojo {

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (configFilePath == null) {
throw new MojoExecutionException("Config file is not defined");
}

final Config cfg;
try {
cfg = Config.loadConfig(new File(configFilePath));
} catch (IOException ex) {
throw new MojoExecutionException("Cannot load configuration from " + configFilePath, ex);
}

cfg.buildSettings.setVersion(warVersion);
if (mvnSettingsFile != null) {
cfg.buildSettings.setMvnSettingsFile(new File(mvnSettingsFile));
}
if (tmpDir != null) {
cfg.buildSettings.setTmpDir(new File(tmpDir));
} else { // Use a Maven temporary dir
//TODO: use step ID
cfg.buildSettings.setTmpDir(new File(project.getBuild().getDirectory(), "custom-war-packager-maven-plugin"));
}
File buildDir = new File(project.getBuild().getDirectory(), "custom-war-packager-maven-plugin");
Config cfg = getConfigOrFail();

if (batchMode) {
cfg.buildSettings.addMavenOption("--batch-mode");
cfg.buildSettings.addMavenOption("-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn");
}

// BOM
cfg.buildSettings.setEnvironmentName(environment);
if (bom != null) {
cfg.buildSettings.setBOM(new File(bom));
}

final Builder bldr = new Builder(cfg);
try {
bldr.build();
} catch (Exception ex) {
throw new MojoExecutionException("Failed to build the custom WAR", ex);
}
build(cfg, buildDir);

projectHelper.attachArtifact(project, "war", cfg.getOutputWar());
projectHelper.attachArtifact(project, "yml", "bom", cfg.getOutputBOM());
}

}