diff --git a/core/src/main/java/hudson/maven/MavenEmbedder.java b/core/src/main/java/hudson/maven/MavenEmbedder.java index ee7240c4f3..3c0539c756 100644 --- a/core/src/main/java/hudson/maven/MavenEmbedder.java +++ b/core/src/main/java/hudson/maven/MavenEmbedder.java @@ -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. + * + *

+ * 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 ); diff --git a/core/src/main/java/hudson/maven/MavenModuleSet.java b/core/src/main/java/hudson/maven/MavenModuleSet.java index 312063855f..42c14830d6 100644 --- a/core/src/main/java/hudson/maven/MavenModuleSet.java +++ b/core/src/main/java/hudson/maven/MavenModuleSet.java @@ -485,28 +485,48 @@ public final class MavenModuleSet extends AbstractMavenProject getMavenArgument(String shortForm, String longForm) { + List args = new ArrayList(); + boolean switchFound=false; + for (String t : Util.tokenize(getGoals())) { + if(switchFound) { + args.add(t); + switchFound = false; + } + else + if(t.equals(shortForm) || t.equals(longForm)) + switchFound=true; + else + if(t.startsWith(shortForm)) { + args.add(t.substring(shortForm.length())); + } + else + if(t.startsWith(longForm)) { + args.add(t.substring(longForm.length())); + } + } + return args; + } + /** * If the list of configured goals contain the "-P" option, * return the configured profiles. Otherwise null. */ public String getProfiles() { - StringBuilder buf = new StringBuilder(); - boolean switchFound=false; - for (String t : Util.tokenize(getGoals())) { - if(switchFound) { - buf.append(',').append(t); - switchFound = false; - } - if(t.equals("-P")) - switchFound=true; - else - if(t.startsWith("-P")) { - // -Px,y,z - buf.append(',').append(t.substring(2)); - } + 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)); } - if(buf.length()==0) return null; - return buf.substring(1); + return props; } /** diff --git a/core/src/main/java/hudson/maven/MavenModuleSetBuild.java b/core/src/main/java/hudson/maven/MavenModuleSetBuild.java index 6f4a8e9251..d06ed29735 100644 --- a/core/src/main/java/hudson/maven/MavenModuleSetBuild.java +++ b/core/src/main/java/hudson/maven/MavenModuleSetBuild.java @@ -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 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 relPath = new HashMap(); MavenUtil.resolveModules(embedder,mp,getRootPath(),relPath,listener); diff --git a/core/src/main/java/hudson/maven/MavenUtil.java b/core/src/main/java/hudson/maven/MavenUtil.java index 714f4a240c..f3aabfbedf 100644 --- a/core/src/main/java/hudson/maven/MavenUtil.java +++ b/core/src/main/java/hudson/maven/MavenUtil.java @@ -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; diff --git a/core/src/main/java/hudson/tasks/Maven.java b/core/src/main/java/hudson/tasks/Maven.java index f6aacee339..dcd91c6460 100644 --- a/core/src/main/java/hudson/tasks/Maven.java +++ b/core/src/main/java/hudson/tasks/Maven.java @@ -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;