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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
54ae52f
[JENKINS-51214] - Allow invoking Custom WAR Packager Maven Plugin in …
oleg-nenashev 4f2214e
[JENKINS-51214] - Split Mojos to Project-dependent and project-indepe…
oleg-nenashev 6fe3118
Merge branch 'master' into JENKINS-51214
oleg-nenashev 0011e9c
[JENKINS-51259] - Add Javadoc and HelpMojo generation
oleg-nenashev File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...ckager-maven-plugin/src/main/java/io/jenkins/tools/warpackager/mavenplugin/BuildMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
// 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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is weird. Delete the option IMO.
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.
why is it weird?
It may be a bit hackish, but it works if a user does not pass proper settings.xml
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.
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.