You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by si...@apache.org on 2009/04/08 17:52:33 UTC

svn commit: r763291 - in /maven/components/trunk: maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/ maven-project/src/main/java/org/apache/maven/profiles/ maven-project/src/main/java/org/apache/maven/profiles/matchers/ maven...

Author: sisbell
Date: Wed Apr  8 15:52:32 2009
New Revision: 763291

URL: http://svn.apache.org/viewvc?rev=763291&view=rev
Log:
[MNG-1957] jdk activation for profiles.

Added:
    maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/matchers/JdkMatcher.java
    maven/components/trunk/maven-project/src/test/java/org/apache/maven/profiles/matchers/
    maven/components/trunk/maven-project/src/test/java/org/apache/maven/profiles/matchers/JdkMatcherTest.java
    maven/components/trunk/maven-project/src/test/resources-project-builder/jdk-activation/
    maven/components/trunk/maven-project/src/test/resources-project-builder/jdk-activation/pom.xml
Modified:
    maven/components/trunk/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/JdkMatcher.java
    maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java
    maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/ProfileActivationContext.java
    maven/components/trunk/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java
    maven/components/trunk/maven-project/src/test/java/org/apache/maven/project/PomConstructionTest.java

Modified: maven/components/trunk/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/JdkMatcher.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/JdkMatcher.java?rev=763291&r1=763290&r2=763291&view=diff
==============================================================================
--- maven/components/trunk/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/JdkMatcher.java (original)
+++ maven/components/trunk/maven-project-builder/src/main/java/org/apache/maven/project/builder/profile/JdkMatcher.java Wed Apr  8 15:52:32 2009
@@ -165,7 +165,7 @@
         return ranges;
     }
 
-    private static class RangeValue
+    public static class RangeValue
     {
         private String value;
 

Modified: maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java?rev=763291&r1=763290&r2=763291&view=diff
==============================================================================
--- maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java (original)
+++ maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java Wed Apr  8 15:52:32 2009
@@ -27,6 +27,7 @@
 import org.apache.maven.profiles.ProfileManager;
 import org.apache.maven.profiles.matchers.DefaultMatcher;
 import org.apache.maven.profiles.matchers.FileMatcher;
+import org.apache.maven.profiles.matchers.JdkMatcher;
 import org.apache.maven.profiles.matchers.ProfileMatcher;
 import org.apache.maven.profiles.matchers.PropertyMatcher;
 import org.apache.maven.shared.model.InterpolatorProperty;
@@ -50,7 +51,7 @@
     private static final ProfileMatcher defaultMatcher = new DefaultMatcher();
 
     private static final List<ProfileMatcher> matchers =
-        (List<ProfileMatcher>) Collections.unmodifiableList( Arrays.asList( new PropertyMatcher(), new FileMatcher() ) );    
+        (List<ProfileMatcher>) Collections.unmodifiableList( Arrays.asList( new PropertyMatcher(), new FileMatcher(), new JdkMatcher() ) );    
 
     /**
      * the properties passed to the profile manager are the props that
@@ -197,11 +198,13 @@
 	    List<Profile> projectProfiles = new ArrayList<Profile>();
 	    ProfileManager externalProfileManager = config.getGlobalProfileManager();
 	    
-	    Properties props = new Properties(config.getExecutionProperties());
+	    Properties props = new Properties();
+	    props.putAll(config.getExecutionProperties());
 	    props.putAll(config.getUserProperties());
 	    
-	    ProfileActivationContext profileActivationContext = (externalProfileManager == null) ? new ProfileActivationContext( props, false ):
+	    ProfileActivationContext profileActivationContext = (externalProfileManager == null) ? new ProfileActivationContext( new Properties(), false ):
 	        externalProfileManager.getProfileActivationContext();
+	    profileActivationContext.getExecutionProperties().putAll(props);
 	    
 	    if(externalProfileManager != null)
 	    {           

Modified: maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/ProfileActivationContext.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/ProfileActivationContext.java?rev=763291&r1=763290&r2=763291&view=diff
==============================================================================
--- maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/ProfileActivationContext.java (original)
+++ maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/ProfileActivationContext.java Wed Apr  8 15:52:32 2009
@@ -40,7 +40,7 @@
 
     public ProfileActivationContext( Properties executionProperties, boolean isCustomActivatorFailureSuppressed )
     {
-        this.executionProperties = executionProperties;
+        this.executionProperties = (executionProperties != null) ? executionProperties : new Properties();
         this.isCustomActivatorFailureSuppressed = isCustomActivatorFailureSuppressed;
     }
 

Added: maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/matchers/JdkMatcher.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/matchers/JdkMatcher.java?rev=763291&view=auto
==============================================================================
--- maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/matchers/JdkMatcher.java (added)
+++ maven/components/trunk/maven-project/src/main/java/org/apache/maven/profiles/matchers/JdkMatcher.java Wed Apr  8 15:52:32 2009
@@ -0,0 +1,191 @@
+package org.apache.maven.profiles.matchers;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.maven.model.Profile;
+import org.apache.maven.shared.model.InterpolatorProperty;
+
+public class JdkMatcher
+	implements ProfileMatcher 
+	{
+	
+    private static final String JDK_VERSION = "${java.version}";
+
+	public boolean isMatch(Profile profile,
+			List<InterpolatorProperty> properties) {
+        String version = null;
+        for(InterpolatorProperty ip : properties)
+        {
+        	if(ip.getKey().equals(JDK_VERSION))
+        	{
+        		version = ip.getValue();
+        		break;
+        	}
+        }
+        if ( version == null )
+        {
+            return false;
+        }
+        
+        org.apache.maven.model.Activation activation = profile.getActivation();
+        if(activation == null || activation.getJdk() == null)
+        {
+        	return false;
+        }
+     
+        String jdk = activation.getJdk();
+        if ( jdk.startsWith( "!" ) )
+        {
+            return !version.startsWith( jdk.replaceFirst( "!", "" ) );
+        }
+        else if ( isRange( jdk ) )
+        {
+            return isInRange( version, getRange( jdk ) );
+        }
+        else
+        {
+            return version.startsWith( jdk );
+        }
+
+	}
+	
+    private static boolean isInRange( String value, List<RangeValue> range )
+    {
+        int leftRelation = getRelationOrder( value, range.get( 0 ), true );
+
+        if ( leftRelation == 0 )
+        {
+            return true;
+        }
+
+        if ( leftRelation < 0 )
+        {
+            return false;
+        }
+
+        return getRelationOrder( value, range.get( 1 ), false ) <= 0;
+    }
+
+    private static int getRelationOrder( String value, RangeValue rangeValue, boolean isLeft )
+    {
+        if ( rangeValue.value.length() <= 0 )
+        {
+            return isLeft ? 1 : -1;
+        }
+
+        List<String> valueTokens = new ArrayList<String>( Arrays.asList( value.split( "\\." ) ) );
+        List<String> rangeValueTokens = new ArrayList<String>( Arrays.asList( rangeValue.value.split( "\\." ) ) );
+
+        int max = Math.max( valueTokens.size(), rangeValueTokens.size() );
+        addZeroTokens( valueTokens, max );
+        addZeroTokens( rangeValueTokens, max );
+
+        if ( value.equals( rangeValue.value ) )
+        {
+            if ( !rangeValue.isClosed() )
+            {
+                return isLeft ? -1 : 1;
+            }
+            return 0;
+        }
+
+        for ( int i = 0; i < valueTokens.size(); i++ )
+        {
+            int x = Integer.parseInt( valueTokens.get( i ) );
+            int y = Integer.parseInt( rangeValueTokens.get( i ) );
+            if ( x < y )
+            {
+                return -1;
+            }
+            else if ( x > y )
+            {
+                return 1;
+            }
+        }
+        if ( !rangeValue.isClosed() )
+        {
+            return isLeft ? -1 : 1;
+        }
+        return 0;
+    }
+
+    private static void addZeroTokens( List<String> tokens, int max )
+    {
+        if ( tokens.size() < max )
+        {
+            for ( int i = 0; i < ( max - tokens.size() ); i++ )
+            {
+                tokens.add( "0" );
+            }
+        }
+    }
+
+    private static boolean isRange( String value )
+    {
+        return value.contains( "," );
+    }
+
+    private static List<RangeValue> getRange( String range )
+    {
+        List<RangeValue> ranges = new ArrayList<RangeValue>();
+
+        for ( String token : range.split( "," ) )
+        {
+            if ( token.startsWith( "[" ) )
+            {
+                ranges.add( new RangeValue( token.replace( "[", "" ), true ) );
+            }
+            else if ( token.startsWith( "(" ) )
+            {
+                ranges.add( new RangeValue( token.replace( "(", "" ), false ) );
+            }
+            else if ( token.endsWith( "]" ) )
+            {
+                ranges.add( new RangeValue( token.replace( "]", "" ), true ) );
+            }
+            else if ( token.endsWith( ")" ) )
+            {
+                ranges.add( new RangeValue( token.replace( ")", "" ), false ) );
+            }
+            else if ( token.length() <= 0 )
+            {
+                ranges.add( new RangeValue( "", false ) );
+            }
+        }
+        if ( ranges.size() < 2 )
+        {
+            ranges.add( new RangeValue( "99999999", false ) );
+        }
+        return ranges;
+    }
+
+    private static class RangeValue
+    {
+        private String value;
+
+        private boolean isClosed;
+
+        RangeValue( String value, boolean isClosed )
+        {
+            this.value = value.trim();
+            this.isClosed = isClosed;
+        }
+
+        public String getValue()
+        {
+            return value;
+        }
+
+        public boolean isClosed()
+        {
+            return isClosed;
+        }
+        
+        public String toString()
+        {
+            return value;
+        }
+    }	
+}

Modified: maven/components/trunk/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java?rev=763291&r1=763290&r2=763291&view=diff
==============================================================================
--- maven/components/trunk/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java (original)
+++ maven/components/trunk/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java Wed Apr  8 15:52:32 2009
@@ -440,8 +440,12 @@
         List<String> inactiveProfileIds = ( projectBuilderConfiguration != null && projectBuilderConfiguration.getGlobalProfileManager() != null && projectBuilderConfiguration
             .getGlobalProfileManager().getProfileActivationContext() != null ) ? projectBuilderConfiguration.getGlobalProfileManager().getProfileActivationContext().getExplicitlyInactiveProfileIds()
                                                                               : new ArrayList<String>();
-
-            ProfileManagerInfo profileInfo = new ProfileManagerInfo(null, activeProfileIds, inactiveProfileIds);
+            
+            List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
+            interpolatorProperties.addAll( InterpolatorProperty.toInterpolatorProperties( projectBuilderConfiguration.getExecutionProperties(), PomInterpolatorTag.EXECUTION_PROPERTIES.name() ) );
+            interpolatorProperties.addAll( InterpolatorProperty.toInterpolatorProperties( projectBuilderConfiguration.getUserProperties(), PomInterpolatorTag.USER_PROPERTIES.name() ) );
+            
+            ProfileManagerInfo profileInfo = new ProfileManagerInfo(interpolatorProperties, activeProfileIds, inactiveProfileIds);
             PomClassicDomainModel domainModel = new PomClassicDomainModel( pomFile );
             domainModel.setProjectDirectory( pomFile.getParentFile() );
             domainModel.setMostSpecialized( true );
@@ -510,6 +514,7 @@
 
             // Lineage count is inclusive to add the POM read in itself.
             transformedDomainModel.setLineageCount( lineageCount + 1 );
+            Model m = transformedDomainModel.getModel();
             transformedDomainModel.setParentFile( parentFile );
 
             return transformedDomainModel;

Added: maven/components/trunk/maven-project/src/test/java/org/apache/maven/profiles/matchers/JdkMatcherTest.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project/src/test/java/org/apache/maven/profiles/matchers/JdkMatcherTest.java?rev=763291&view=auto
==============================================================================
--- maven/components/trunk/maven-project/src/test/java/org/apache/maven/profiles/matchers/JdkMatcherTest.java (added)
+++ maven/components/trunk/maven-project/src/test/java/org/apache/maven/profiles/matchers/JdkMatcherTest.java Wed Apr  8 15:52:32 2009
@@ -0,0 +1,24 @@
+package org.apache.maven.profiles.matchers;
+
+import java.util.Collections;
+
+import org.apache.maven.model.Activation;
+import org.apache.maven.model.Profile;
+import org.apache.maven.shared.model.InterpolatorProperty;
+
+import junit.framework.TestCase;
+
+public class JdkMatcherTest extends TestCase
+{
+	public void testJdkMatch()
+		throws Exception
+	{ 
+		Profile p = new Profile();
+		Activation a = new Activation();
+		a.setJdk("(1.3,100)");
+		p.setActivation(a);
+		
+		JdkMatcher m = new JdkMatcher();
+		assertTrue(m.isMatch(p, Collections.singletonList(new InterpolatorProperty("${java.version}", "1.5.0_16"))));
+	}
+}

Modified: maven/components/trunk/maven-project/src/test/java/org/apache/maven/project/PomConstructionTest.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project/src/test/java/org/apache/maven/project/PomConstructionTest.java?rev=763291&r1=763290&r2=763291&view=diff
==============================================================================
--- maven/components/trunk/maven-project/src/test/java/org/apache/maven/project/PomConstructionTest.java (original)
+++ maven/components/trunk/maven-project/src/test/java/org/apache/maven/project/PomConstructionTest.java Wed Apr  8 15:52:32 2009
@@ -23,6 +23,7 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -876,14 +877,10 @@
     }
 
     /** MNG-4027*/
-    /* FIXME*/
-
     public void testProfileInjectedDependencies()
         throws Exception
     {
-    	//c.b,d,a
         PomTestWrapper pom = buildPom( "profile-injected-dependencies" );
-        System.out.println(pom.getDomainModel().asString());
         assertEquals( 4, ( (List<?>) pom.getValue( "dependencies" ) ).size() );
         assertEquals( "a", pom.getValue( "dependencies[1]/artifactId" ) );
         assertEquals( "c", pom.getValue( "dependencies[2]/artifactId" ) );
@@ -891,18 +888,14 @@
         assertEquals( "d", pom.getValue( "dependencies[4]/artifactId" ) );
     }
     
-
     public void testDependencyInheritance()
         throws Exception
     {
-    	//c.b,d,a
         PomTestWrapper pom = buildPom( "dependency-inheritance/sub" );
         assertEquals(1,  ( (List<?>) pom.getValue( "dependencies" ) ).size() );
         assertEquals("4.4",  pom.getValue("dependencies[1]/version") );
-        System.out.println(pom.getDomainModel().asString());
     }
   
-//*/
     /** MNG-4034 */
     public void testManagedProfileDependency()
         throws Exception
@@ -1334,6 +1327,20 @@
         assertEquals("c", pom.getValue( "build/extensions[3]/artifactId" ) );
     }
     
+    /*MNG-1957*/
+    public void testJdkActivation()
+    	throws Exception
+	{
+    	Properties props = new Properties();
+	    props.put("java.version", "1.5.0_15");
+	    	
+	    PomTestWrapper pom = buildPom( "jdk-activation",  props ); 
+	    assertEquals(3, pom.getMavenProject().getActiveProfiles().size());	
+	    assertEquals("PASSED", pom.getValue("properties/jdkProperty3"));
+	    assertEquals("PASSED", pom.getValue("properties/jdkProperty2"));
+	    assertEquals("PASSED", pom.getValue("properties/jdkProperty1"));
+	}   
+    
     /* MNG-2174 */
     public void testProfilePluginMngDependencies()
         throws Exception
@@ -1372,7 +1379,24 @@
     {
         assertEquals( new File( value.toString() ).getPath(), value.toString() );
     }
-
+    
+    private PomTestWrapper buildPom( String pomPath, Properties properties)
+    throws Exception
+{
+    File pomFile = new File( testDirectory , pomPath );
+    if ( pomFile.isDirectory() )
+    {
+        pomFile = new File( pomFile, "pom.xml" );
+    }
+    ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration();
+    config.setLocalRepository(new DefaultArtifactRepository("default", "", new DefaultRepositoryLayout()));
+    ProfileActivationContext pCtx = new ProfileActivationContext(null, true);
+
+    config.setExecutionProperties(properties);
+    config.setGlobalProfileManager(new DefaultProfileManager(this.getContainer(), pCtx));
+    return new PomTestWrapper( pomFile, mavenProjectBuilder.build( pomFile, config ) );
+}
+    
     private PomTestWrapper buildPom( String pomPath, String... profileIds )
         throws Exception
     {

Added: maven/components/trunk/maven-project/src/test/resources-project-builder/jdk-activation/pom.xml
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-project/src/test/resources-project-builder/jdk-activation/pom.xml?rev=763291&view=auto
==============================================================================
--- maven/components/trunk/maven-project/src/test/resources-project-builder/jdk-activation/pom.xml (added)
+++ maven/components/trunk/maven-project/src/test/resources-project-builder/jdk-activation/pom.xml Wed Apr  8 15:52:32 2009
@@ -0,0 +1,105 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.mng1957</groupId>
+  <artifactId>test</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <name>Maven Integration Test :: MNG-1957</name>
+  <description>
+    Test that JDK profile activation allows version ranges.
+  </description>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.its.plugins</groupId>
+        <artifactId>maven-it-plugin-expression</artifactId>
+        <version>2.1-SNAPSHOT</version>
+        <executions>
+          <execution>
+            <phase>validate</phase>
+            <goals>
+              <goal>eval</goal>
+            </goals>
+            <configuration>
+              <outputFile>target/jdk.properties</outputFile>
+              <expressions>
+                <expression>project/properties</expression>
+              </expressions>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+  <profiles>
+    <profile>
+      <id>test-1</id>
+      <activation>
+        <jdk>[1.4,)</jdk>
+      </activation>
+      <properties>
+        <jdkProperty1>PASSED</jdkProperty1>
+      </properties>
+    </profile>
+    <profile>
+      <id>test-2</id>
+      <activation>
+        <jdk>(,100)</jdk>
+      </activation>
+      <properties>
+        <jdkProperty2>PASSED</jdkProperty2>
+      </properties>
+    </profile>
+    <profile>
+      <id>test-3</id>
+      <activation>
+        <jdk>(1.3,100)</jdk>
+      </activation>
+      <properties>
+        <jdkProperty3>PASSED</jdkProperty3>
+      </properties>
+    </profile>
+    <profile>
+      <id>test-4</id>
+      <activation>
+        <jdk>(100,)</jdk>
+      </activation>
+      <properties>
+        <jdkProperty4>FAILED</jdkProperty4>
+      </properties>
+    </profile>
+    <profile>
+      <id>test-5</id>
+      <activation>
+        <jdk>(,1.4)</jdk>
+      </activation>
+      <properties>
+        <jdkProperty5>FAILED</jdkProperty5>
+      </properties>
+    </profile>
+  </profiles>
+</project>