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
Changes from 3 commits
54ae52f
4f2214e
6fe3118
0011e9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
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"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is weird. Delete the option IMO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is it weird? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the calling script should be responsible for including |
||
|
||
// 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; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For sure. Created https://issues.jenkins-ci.org/browse/JENKINS-51259