提交 ef3fd824 编写于 作者: N Nigel Magnay

Allow maven builds to (opionally) make use of the token-macro-plugin.

It is useful to be able to pass parameters in the form of
-Doption=${THING}
to the maven builds.

The new token-macro-plugin stuff from KK is ideal for this.

To get the actual parameters through to the launch commandline requires
some adjustments to the Launcher API, because it is not aware of the
build itself, which is neccessary to do the options expansion. So instead
pass these through to the launcher itself.

If the token-macro-plugin is not installed, the behaviour remains as before.
Signed-off-by: NNigel Magnay <nigel.magnay@gmail.com>
上级 edea4982
......@@ -253,6 +253,14 @@ THE SOFTWARE.
<artifactId>nekohtml</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>1.0</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
......
......@@ -38,6 +38,7 @@ import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.stapler.framework.io.IOException2;
/*
......@@ -88,11 +89,14 @@ public abstract class AbstractMavenProcessFactory
*/
private final FilePath workDir;
AbstractMavenProcessFactory(MavenModuleSet mms, Launcher launcher, EnvVars envVars, FilePath workDir) {
private final String mavenOpts;
AbstractMavenProcessFactory(MavenModuleSet mms, Launcher launcher, EnvVars envVars, String mavenOpts, FilePath workDir) {
this.mms = mms;
this.launcher = launcher;
this.envVars = envVars;
this.workDir = workDir;
this.mavenOpts = mavenOpts;
}
/**
......@@ -243,8 +247,11 @@ public abstract class AbstractMavenProcessFactory
*/
protected abstract ArgumentListBuilder buildMavenAgentCmdLine(BuildListener listener,int tcpPort)
throws IOException, InterruptedException;
public String getMavenOpts() {
if( this.mavenOpts != null )
return this.mavenOpts;
String mavenOpts = mms.getMavenOpts();
if ((mavenOpts==null) || (mavenOpts.trim().length()==0)) {
......@@ -268,6 +275,7 @@ public abstract class AbstractMavenProcessFactory
return envVars.expand(mavenOpts);
}
public MavenInstallation getMavenInstallation(TaskListener log) throws IOException, InterruptedException {
MavenInstallation mi = mms.getMaven();
if (mi != null) mi = mi.forNode(getCurrentNode(), log).forEnvironment(envVars);
......
......@@ -51,8 +51,8 @@ import org.jvnet.hudson.maven3.launcher.Maven3Launcher;
public class Maven3ProcessFactory extends AbstractMavenProcessFactory implements ProcessCache.Factory
{
Maven3ProcessFactory(MavenModuleSet mms, Launcher launcher, EnvVars envVars, FilePath workDir) {
super( mms, launcher, envVars, workDir );
Maven3ProcessFactory(MavenModuleSet mms, Launcher launcher, EnvVars envVars, String mavenOpts, FilePath workDir) {
super( mms, launcher, envVars, mavenOpts, workDir );
}
/**
* Builds the command line argument list to launch the maven process.
......
......@@ -52,6 +52,7 @@ import org.apache.maven.execution.ReactorManager;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.monitor.event.EventDispatcher;
import org.apache.maven.project.MavenProject;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
......@@ -598,14 +599,14 @@ public class MavenBuild extends AbstractMavenBuild<MavenModule,MavenBuild> {
process =
MavenBuild.mavenProcessCache.get( launcher.getChannel(), listener,
new Maven3ProcessFactory( getParent().getParent(), launcher,
envVars, null ) );
envVars, getMavenOpts(listener), null ) );
}
else
{
process =
MavenBuild.mavenProcessCache.get( launcher.getChannel(), listener,
new MavenProcessFactory( getParent().getParent(), launcher,
envVars, null ) );
envVars, getMavenOpts(listener), null ) );
}
......@@ -674,6 +675,23 @@ public class MavenBuild extends AbstractMavenBuild<MavenModule,MavenBuild> {
}
public String getMavenOpts(TaskListener listener) {
MavenModuleSet mms = getProject().getParent();
String opts = mms.getMavenOpts();
try {
opts = TokenMacro.expand(this, listener, opts);
}
catch(Exception tokenException) {
listener.error("Problem expanding maven opts macros");
}
catch(LinkageError linkageError) {
// Token plugin not present. Ignore, this is OK.
}
return opts;
}
private static final int MAX_PROCESS_CACHE = 5;
protected static final ProcessCache mavenProcessCache = new ProcessCache(MAX_PROCESS_CACHE);
......
......@@ -35,22 +35,8 @@ import hudson.Util;
import hudson.maven.MavenBuild.ProxyImpl2;
import hudson.maven.reporters.MavenFingerprinter;
import hudson.maven.reporters.MavenMailer;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Build;
import hudson.model.BuildListener;
import hudson.model.*;
import hudson.model.Cause.UpstreamCause;
import hudson.model.Computer;
import hudson.model.Environment;
import hudson.model.Fingerprint;
import hudson.model.Hudson;
import hudson.model.ParameterDefinition;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.StringParameterDefinition;
import hudson.model.TaskListener;
import hudson.remoting.Channel;
import hudson.remoting.VirtualChannel;
import hudson.scm.ChangeLogSet;
......@@ -94,6 +80,8 @@ import org.apache.maven.monitor.event.EventDispatcher;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.plexus.util.PathTool;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.sonatype.aether.transfer.TransferCancelledException;
......@@ -153,7 +141,8 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven
@Override
public EnvVars getEnvironment(TaskListener log) throws IOException, InterruptedException {
EnvVars envs = super.getEnvironment(log);
String opts = project.getMavenOpts();
String opts = getMavenOpts(log);
if(opts!=null)
envs.put("MAVEN_OPTS", opts);
// We need to add M2_HOME and the mvn binary to the PATH so if Maven
......@@ -510,6 +499,22 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven
}
}
public String getMavenOpts(TaskListener listener) {
String opts = project.getMavenOpts();
try {
opts = TokenMacro.expand(this, listener, opts);
}
catch(Exception tokenException) {
listener.error("Problem expanding maven opts macros");
}
catch(LinkageError linkageError) {
// Token plugin not present. Ignore, this is OK.
}
return opts;
}
/**
* The sole job of the {@link MavenModuleSet} build is to update SCM
* and triggers module builds.
......@@ -615,14 +620,14 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven
LOGGER.info( "using maven 3 " + mavenVersion );
process =
MavenBuild.mavenProcessCache.get( launcher.getChannel(), slistener,
new Maven3ProcessFactory( project, launcher, envVars,
new Maven3ProcessFactory( project, launcher, envVars, getMavenOpts(listener),
pom.getParent() ) );
}
else
{
process =
MavenBuild.mavenProcessCache.get( launcher.getChannel(), slistener,
new MavenProcessFactory( project, launcher, envVars,
new MavenProcessFactory( project, launcher, envVars,getMavenOpts(listener),
pom.getParent() ) );
}
ArgumentListBuilder margs = new ArgumentListBuilder().add("-B").add("-f", pom.getRemote());
......
......@@ -78,8 +78,8 @@ import java.util.logging.Logger;
final class MavenProcessFactory extends AbstractMavenProcessFactory implements ProcessCache.Factory {
MavenProcessFactory(MavenModuleSet mms, Launcher launcher, EnvVars envVars, FilePath workDir) {
super( mms, launcher, envVars, workDir );
MavenProcessFactory(MavenModuleSet mms, Launcher launcher, EnvVars envVars, String mavenOpts, FilePath workDir) {
super( mms, launcher, envVars, mavenOpts, workDir );
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册