support for environment variables in native Maven builds

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@15927 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
huybrechts committed 2009-03-01 20:43:52 +00:00
1 parent 7aea741545
commit 3370ee3e31
10 files changed
+179 -124

No files matched your search

+8 -3
View File
@@ -91,10 +91,15 @@ public class EnvVars extends TreeMap<String,String> {
put(key,value);
}
public void overrideAll(Map<String,String> all) {
/**
* Overrides all values in the map. See #override
* @return this
*/
public EnvVars overrideAll(Map<String,String> all) {
for (Map.Entry<String, String> e : all.entrySet()) {
override(e.getKey(),e.getValue());
}
return this;
}
/**
@@ -129,9 +134,9 @@ public class EnvVars extends TreeMap<String,String> {
* @param channel
* Can be null, in which case the map indicating "N/A" will be returned.
*/
public static Map<String,String> getRemote(VirtualChannel channel) throws IOException, InterruptedException {
public static EnvVars getRemote(VirtualChannel channel) throws IOException, InterruptedException {
if(channel==null)
return Collections.singletonMap("N/A","N/A");
return new EnvVars("N/A","N/A");
return new EnvVars(channel.call(new GetEnvVars()));
}
@@ -25,6 +25,7 @@ package hudson.maven;
import hudson.FilePath;
import hudson.Util;
import hudson.EnvVars;
import hudson.maven.agent.AbortException;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
@@ -454,8 +455,8 @@ public class MavenBuild extends AbstractBuild<MavenModule,MavenBuild> {
if(debug)
listener.getLogger().println("Reporters="+reporters);
Map<String,String> envVars = getEnvVars();
EnvVars envVars = EnvVars.getRemote(launcher.getChannel()).overrideAll(getEnvVars());
ProcessCache.MavenProcess process = mavenProcessCache.get(launcher.getChannel(), listener,
new MavenProcessFactory(getParent().getParent(),launcher,envVars,null));
@@ -470,6 +470,10 @@ public final class MavenModuleSet extends AbstractMavenProject<MavenModuleSet,Ma
return rootPOM;
}
public void setRootPOM(String rootPOM) {
this.rootPOM = rootPOM;
}
public AbstractProject<?,?> asProject() {
return this;
}
@@ -485,6 +489,10 @@ public final class MavenModuleSet extends AbstractMavenProject<MavenModuleSet,Ma
return goals;
}
public void setGoals(String goals) {
this.goals = goals;
}
private List<String> getMavenArgument(String shortForm, String longForm) {
List<String> args = new ArrayList<String>();
boolean switchFound=false;
@@ -549,6 +557,10 @@ public final class MavenModuleSet extends AbstractMavenProject<MavenModuleSet,Ma
return null;
}
public void setMaven(String mavenName) {
this.mavenName = mavenName;
}
/**
* Returns the {@link MavenModule}s that are in the queue.
*/
@@ -27,6 +27,7 @@ import hudson.AbortException;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.EnvVars;
import hudson.FilePath.FileCallable;
import hudson.maven.MavenBuild.ProxyImpl2;
import hudson.maven.reporters.MavenFingerprinter;
@@ -40,6 +41,7 @@ import hudson.model.Fingerprint;
import hudson.model.Hudson;
import hudson.model.ParametersAction;
import hudson.model.Result;
import hudson.model.Computer;
import hudson.model.Cause.UpstreamCause;
import hudson.remoting.Channel;
import hudson.remoting.VirtualChannel;
@@ -309,7 +311,8 @@ public final class MavenModuleSetBuild extends AbstractBuild<MavenModuleSet,Mave
protected Result doRun(final BuildListener listener) throws Exception {
PrintStream logger = listener.getLogger();
try {
parsePoms(listener, logger);
EnvVars envVars = EnvVars.getRemote(launcher.getChannel());
parsePoms(listener, logger, envVars);
if(!project.isAggregatorStyleBuild()) {
// start module builds
@@ -341,18 +344,20 @@ public final class MavenModuleSetBuild extends AbstractBuild<MavenModuleSet,Mave
for (MavenModule m : project.sortedActiveModules)
proxies.put(m.getModuleName(),m.newBuild().new ProxyImpl2(MavenModuleSetBuild.this,slistener));
envVars.overrideAll(getEnvVars());
// run the complete build here
// figure out the root POM location.
// choice of module root ('ws' in this method) is somewhat arbitrary
// when multiple CVS/SVN modules are checked out, so also check
// the path against the workspace root if that seems like what the user meant (see issue #1293)
FilePath pom = project.getModuleRoot().child(project.getRootPOM());
FilePath parentLoc = project.getWorkspace().child(project.getRootPOM());
String rootPOM = project.getRootPOM();
FilePath pom = project.getModuleRoot().child(rootPOM);
FilePath parentLoc = project.getWorkspace().child(rootPOM);
if(!pom.exists() && parentLoc.exists())
pom = parentLoc;
Map<String,String> envVars = getEnvVars();
ProcessCache.MavenProcess process = MavenBuild.mavenProcessCache.get(launcher.getChannel(), slistener,
new MavenProcessFactory(project,launcher,envVars,pom.getParent()));
@@ -360,7 +365,7 @@ public final class MavenModuleSetBuild extends AbstractBuild<MavenModuleSet,Mave
margs.add("-B").add("-f", pom.getRemote());
if(project.usesPrivateRepository())
margs.add("-Dmaven.repo.local="+project.getWorkspace().child(".repository"));
margs.addTokenized(project.getGoals());
margs.addTokenized(envVars.expand(project.getGoals()));
Builder builder = new Builder(slistener, proxies, project.sortedActiveModules, margs.toList(), envVars);
MavenProbeAction mpa=null;
@@ -407,13 +412,14 @@ public final class MavenModuleSetBuild extends AbstractBuild<MavenModuleSet,Mave
}
}
private void parsePoms(BuildListener listener, PrintStream logger) throws IOException, InterruptedException {
private void parsePoms(BuildListener listener, PrintStream logger, EnvVars envVars) throws IOException, InterruptedException {
logger.println("Parsing POMs");
MavenInstallation mvn = project.getMaven();
if(mvn==null) {
logger.println("A Maven configuration needs to be associated with this project. Please reconfigure this project");
throw new AbortException();
}
mvn = mvn.forEnvironment(envVars).forNode(Computer.currentComputer().getNode());
List<PomInfo> poms;
try {
@@ -27,6 +27,7 @@ import hudson.FilePath;
import hudson.Launcher;
import hudson.Proc;
import hudson.AbortException;
import hudson.EnvVars;
import static hudson.Util.fixNull;
import hudson.maven.agent.Main;
import hudson.model.BuildListener;
@@ -81,7 +82,7 @@ final class MavenProcessFactory implements ProcessCache.Factory {
* Environment variables to be set to the maven process.
* The same variables are exposed to the system property as well.
*/
private final Map<String,String> envVars;
private final EnvVars envVars;
/**
* Optional working directory. Because of the process reuse, we can't always guarantee
@@ -93,7 +94,7 @@ final class MavenProcessFactory implements ProcessCache.Factory {
*/
private final FilePath workDir;
MavenProcessFactory(MavenModuleSet mms, Launcher launcher, Map<String, String> envVars, FilePath workDir) {
MavenProcessFactory(MavenModuleSet mms, Launcher launcher, EnvVars envVars, FilePath workDir) {
this.mms = mms;
this.launcher = launcher;
this.envVars = envVars;
@@ -281,11 +282,12 @@ final class MavenProcessFactory implements ProcessCache.Factory {
slaveRoot = getCurrentNode().getRootPath();
ArgumentListBuilder args = new ArgumentListBuilder();
JDK jdk = mms.getJDK();
if(jdk==null)
JDK jdk = getJava();
if(jdk==null) {
args.add("java");
else
args.add(jdk.getJavaHome()+"/bin/java");
} else {
args.add(jdk.getJavaHome()+"/bin/java"); // use JDK.getExecutable() here ?
}
if(debugPort!=0)
args.add("-Xrunjdwp:transport=dt_socket,server=y,address="+debugPort);
@@ -320,22 +322,20 @@ final class MavenProcessFactory implements ProcessCache.Factory {
}
public String getMavenOpts() {
String opts = mms.getMavenOpts();
if (opts == null)
return null;
for (String key : envVars.keySet())
opts = opts.replace("${" + key + "}", envVars.get(key));
return opts;
return envVars.expand(mms.getMavenOpts());
}
public MavenInstallation getMavenInstallation() {
return mms.getMaven();
MavenInstallation mi = mms.getMaven();
if (mi != null) mi = mi.forNode(getCurrentNode()).forEnvironment(envVars);
return mi;
}
public JDK getJava() {
return mms.getJDK();
JDK jdk = mms.getJDK();
if (jdk != null) jdk = jdk.forNode(getCurrentNode()).forEnvironment(envVars);
return jdk;
}
/**
+6 -1
View File
@@ -27,6 +27,7 @@ import hudson.util.StreamTaskListener;
import hudson.util.NullStream;
import hudson.Launcher;
import hudson.Extension;
import hudson.EnvVars;
import hudson.slaves.NodeSpecific;
import hudson.tools.ToolInstallation;
import hudson.tools.ToolDescriptor;
@@ -42,7 +43,7 @@ import java.util.List;
*
* @author Kohsuke Kawaguchi
*/
public final class JDK extends ToolInstallation implements NodeSpecific<JDK> {
public final class JDK extends ToolInstallation implements NodeSpecific<JDK>, EnvironmentSpecific<JDK> {
@Deprecated // kept for backward compatibility - use getHome() instead
private String javaHome;
@@ -104,6 +105,10 @@ public final class JDK extends ToolInstallation implements NodeSpecific<JDK> {
return new JDK(getName(),ToolLocationNodeProperty.getToolHome(node, this));
}
public JDK forEnvironment(EnvVars environment) {
return new JDK(getName(), environment.expand(getHome()));
}
/**
* Checks if "java" is in PATH on the given node.
*
+1 -3
View File
@@ -209,13 +209,11 @@ public class Maven extends Builder {
ArgumentListBuilder args = new ArgumentListBuilder();
MavenInstallation mi = getMaven();
if (mi != null) {
mi = mi.forNode(Computer.currentComputer().getNode());
}
if(mi==null) {
String execName = proj.getWorkspace().act(new DecideDefaultMavenCommand(normalizedTarget));
args.add(execName);
} else {
mi = mi.forNode(Computer.currentComputer().getNode());
mi = mi.forEnvironment(env);
String exec = mi.getExecutable(launcher);
if(exec==null) {