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 3 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
@@ -0,0 +1,105 @@
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.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 {

@Parameter(property = "configFile", required = true)
public @CheckForNull String configFilePath;

@Parameter(property = "version")
public @CheckForNull String warVersion;

@Parameter(property = "tmpDir")
Copy link
Member

Choose a reason for hiding this comment

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

BTW if you add Javadoc for these things, and configure your POM to generate HelpMojo, you will make it easier for people to invoke this.

Copy link
Member Author

Choose a reason for hiding this comment

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

public @CheckForNull String tmpDir;

@Parameter(property = "mvnSettingsFile")
public @CheckForNull String mvnSettingsFile;

@Parameter(property = "bomFile")
public @CheckForNull String bom;

@Parameter(property = "environment")
public @CheckForNull String environment;

@Parameter(property = "batchMode")
public boolean batchMode;

@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());
}

}