提交 48103818 编写于 作者: K kohsuke

[FIXED HUDSON-1454]

Handled the property-based activation correctly. Will be in 1.283.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@15169 71c3de6d-444a-0410-be80-ed276b4c234a
上级 e0b680cb
......@@ -163,6 +163,8 @@ public class MavenEmbedder
private String profiles;
private Properties systemProperties;
/**
* This option determines whether the embedder is to be aligned to the user
* installation.
......@@ -238,6 +240,20 @@ public class MavenEmbedder
this.profiles = profiles;
}
/**
* Sets the properties that the embedded Maven sees as system properties.
*
* <p>
* In various places inside Maven, {@link System#getProperties()} are still referenced,
* and still in other places, the values given to this method is only used as overrides
* and not the replacement of the real {@link System#getProperties()}. So Maven still
* doesn't quite behave as it should, but at least this allows Hudson to add system properties
* to Maven without really messing up our current JVM.
*/
public void setSystemProperties(Properties props) {
this.systemProperties = props;
}
/**
* Set the classloader to use with the maven embedder.
*
......@@ -664,7 +680,7 @@ public class MavenEmbedder
pluginDescriptorBuilder = new PluginDescriptorBuilder();
profileManager = new DefaultProfileManager( embedder.getContainer() );
profileManager = new DefaultProfileManager( embedder.getContainer(), systemProperties );
activeProfiles();
mavenProjectBuilder = (MavenProjectBuilder) embedder.lookup( MavenProjectBuilder.ROLE );
......
......@@ -485,28 +485,48 @@ public final class MavenModuleSet extends AbstractMavenProject<MavenModuleSet,Ma
return goals;
}
/**
* If the list of configured goals contain the "-P" option,
* return the configured profiles. Otherwise null.
*/
public String getProfiles() {
StringBuilder buf = new StringBuilder();
private List<String> getMavenArgument(String shortForm, String longForm) {
List<String> args = new ArrayList<String>();
boolean switchFound=false;
for (String t : Util.tokenize(getGoals())) {
if(switchFound) {
buf.append(',').append(t);
args.add(t);
switchFound = false;
}
if(t.equals("-P"))
else
if(t.equals(shortForm) || t.equals(longForm))
switchFound=true;
else
if(t.startsWith("-P")) {
// -Px,y,z
buf.append(',').append(t.substring(2));
if(t.startsWith(shortForm)) {
args.add(t.substring(shortForm.length()));
}
else
if(t.startsWith(longForm)) {
args.add(t.substring(longForm.length()));
}
}
if(buf.length()==0) return null;
return buf.substring(1);
return args;
}
/**
* If the list of configured goals contain the "-P" option,
* return the configured profiles. Otherwise null.
*/
public String getProfiles() {
return Util.join(getMavenArgument("-P","--activate-profiles"),",");
}
/**
* Gets the system properties explicitly set in the Maven command line (the "-D" option.)
*/
public Properties getMavenProperties() {
Properties props = new Properties();
for (String arg : getMavenArgument("-D","--define")) {
int idx = arg.indexOf('=');
if(idx<0) props.put(arg,"true");
else props.put(arg.substring(0,idx),arg.substring(idx+1));
}
return props;
}
/**
......
......@@ -70,6 +70,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -410,7 +411,7 @@ public final class MavenModuleSetBuild extends AbstractBuild<MavenModuleSet,Mave
List<PomInfo> poms;
try {
poms = project.getModuleRoot().act(new PomParser(listener, mvn,project.getRootPOM(),project.getProfiles()));
poms = project.getModuleRoot().act(new PomParser(listener, mvn, project));
} catch (IOException e) {
if (e.getCause() instanceof AbortException)
throw (AbortException) e.getCause();
......@@ -670,12 +671,15 @@ public final class MavenModuleSetBuild extends AbstractBuild<MavenModuleSet,Mave
private final boolean versbose = debug;
private final MavenInstallation mavenHome;
private final String profiles;
private final Properties properties;
public PomParser(BuildListener listener, MavenInstallation mavenHome, String rootPOM, String profiles) {
public PomParser(BuildListener listener, MavenInstallation mavenHome, MavenModuleSet project) {
// project cannot be shipped to the remote JVM, so all the relevant properties need to be captured now.
this.listener = listener;
this.mavenHome = mavenHome;
this.rootPOM = rootPOM;
this.profiles = profiles;
this.rootPOM = project.getRootPOM();
this.profiles = project.getProfiles();
this.properties = project.getMavenProperties();
}
/**
......@@ -711,7 +715,7 @@ public final class MavenModuleSetBuild extends AbstractBuild<MavenModuleSet,Mave
logger.println("Parsing "+pom);
try {
MavenEmbedder embedder = mavenHome.createEmbedder(listener,profiles);
MavenEmbedder embedder = mavenHome.createEmbedder(listener,profiles,properties);
MavenProject mp = embedder.readProject(pom);
Map<MavenProject,String> relPath = new HashMap<MavenProject,String>();
MavenUtil.resolveModules(embedder,mp,getRootPath(),relPath,listener);
......
......@@ -40,6 +40,7 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author Kohsuke Kawaguchi
......@@ -67,6 +68,10 @@ public class MavenUtil {
return createEmbedder(listener,m!=null?m.getHomeDir():null,profiles);
}
public static MavenEmbedder createEmbedder(TaskListener listener, File mavenHome, String profiles) throws MavenEmbedderException, IOException {
return createEmbedder(listener,mavenHome,profiles,new Properties());
}
/**
* Creates a fresh {@link MavenEmbedder} instance.
*
......@@ -77,8 +82,10 @@ public class MavenUtil {
* from here. Can be null.
* @param profiles
* Profiles to activate/deactivate. Can be null.
* @param systemProperties
* The system properties that the embedded Maven sees. See {@link MavenEmbedder#setSystemProperties(Properties)}.
*/
public static MavenEmbedder createEmbedder(TaskListener listener, File mavenHome, String profiles) throws MavenEmbedderException, IOException {
public static MavenEmbedder createEmbedder(TaskListener listener, File mavenHome, String profiles, Properties systemProperties) throws MavenEmbedderException, IOException {
MavenEmbedder maven = new MavenEmbedder(mavenHome);
ClassLoader cl = MavenUtil.class.getClassLoader();
......@@ -95,6 +102,7 @@ public class MavenUtil {
}
maven.setProfiles(profiles);
maven.setSystemProperties(systemProperties);
maven.start();
return maven;
......
......@@ -397,8 +397,8 @@ public class Maven extends Builder {
}
}
public MavenEmbedder createEmbedder(BuildListener listener, String profiles) throws MavenEmbedderException, IOException {
return MavenUtil.createEmbedder(listener,getHomeDir(),profiles);
public MavenEmbedder createEmbedder(BuildListener listener, String profiles, Properties systemProperties) throws MavenEmbedderException, IOException {
return MavenUtil.createEmbedder(listener,getHomeDir(),profiles,systemProperties);
}
private static final long serialVersionUID = 1L;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册