From c6529932f9e3efdfc86ed73f59a307a8f8b6ea5f Mon Sep 17 00:00:00 2001 From: Mysterion Date: Tue, 27 May 2014 00:18:08 +0400 Subject: [PATCH] Add solution to http://jira.codehaus.org/browse/MNG-4565 Signed-off-by: Jason van Zyl --- .../model/profile/DefaultProfileSelector.java | 12 +++- .../activation/FileProfileActivator.java | 19 ++++++ .../JdkVersionProfileActivator.java | 19 ++++++ .../OperatingSystemProfileActivator.java | 19 ++++++ .../profile/activation/ProfileActivator.java | 13 ++++ .../activation/PropertyProfileActivator.java | 19 ++++++ .../model/building/ComplexActivationTest.java | 60 +++++++++++++++++++ .../test/resources/poms/factory/complex.xml | 49 +++++++++++++++ 8 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 maven-model-builder/src/test/java/org/apache/maven/model/building/ComplexActivationTest.java create mode 100644 maven-model-builder/src/test/resources/poms/factory/complex.xml diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java index c376c999e3..0aeed9d1e9 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java @@ -104,13 +104,19 @@ public class DefaultProfileSelector private boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems ) { + boolean isActive = false; + for ( ProfileActivator activator : activators ) { + if ( activator.presentInConfig( profile, context, problems ) ) { + isActive = true; + } + } for ( ProfileActivator activator : activators ) { try { - if ( activator.isActive( profile, context, problems ) ) + if ( activator.presentInConfig( profile, context, problems ) ) { - return true; + isActive &= activator.isActive( profile, context, problems ); } } catch ( RuntimeException e ) @@ -122,7 +128,7 @@ public class DefaultProfileSelector return false; } } - return false; + return isActive; } private boolean isActiveByDefault( Profile profile ) diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/FileProfileActivator.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/FileProfileActivator.java index 07ba79b4a4..b1d04427a9 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/FileProfileActivator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/FileProfileActivator.java @@ -167,4 +167,23 @@ public class FileProfileActivator return missing ? !fileExists : fileExists; } + @Override + public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems ) + { + Activation activation = profile.getActivation(); + + if ( activation == null ) + { + return false; + } + + ActivationFile file = activation.getFile(); + + if ( file == null ) + { + return false; + } + return true; + } + } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/JdkVersionProfileActivator.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/JdkVersionProfileActivator.java index 62b6cfb7b5..10747de6be 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/JdkVersionProfileActivator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/JdkVersionProfileActivator.java @@ -83,6 +83,25 @@ public class JdkVersionProfileActivator } } + @Override + public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems ) + { + Activation activation = profile.getActivation(); + + if ( activation == null ) + { + return false; + } + + String jdk = activation.getJdk(); + + if ( jdk == null ) + { + return false; + } + return true; + } + private static boolean isInRange( String value, List range ) { int leftRelation = getRelationOrder( value, range.get( 0 ), true ); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java index 94d380cd1a..b6d3f052b5 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java @@ -76,6 +76,25 @@ public class OperatingSystemProfileActivator return active; } + @Override + public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems ) + { + Activation activation = profile.getActivation(); + + if ( activation == null ) + { + return false; + } + + ActivationOS os = activation.getOs(); + + if ( os == null ) + { + return false; + } + return true; + } + private boolean ensureAtLeastOneNonNull( ActivationOS os ) { return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null; diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/ProfileActivator.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/ProfileActivator.java index 142dddf6b6..7094a3f863 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/ProfileActivator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/ProfileActivator.java @@ -43,4 +43,17 @@ public interface ProfileActivator */ boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems ); + /** + * Determines whether specified activation method is present in configuration or not. It should help to have AND between + * activation conditions + * Need for solving http://jira.codehaus.org/browse/MNG-4565 + * @param profile The profile whose activation status should be determined, must not be {@code null}. + * @param context The environmental context used to determine the activation status of the profile, must not be + * {@code null}. + * @param problems The container used to collect problems (e.g. bad syntax) that were encountered, must not be + * {@code null}. + * @return {@code true} if the profile is active, {@code false} otherwise. + */ + boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems ); + } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/PropertyProfileActivator.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/PropertyProfileActivator.java index 374647fd2a..e8e6e998ef 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/PropertyProfileActivator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/PropertyProfileActivator.java @@ -103,4 +103,23 @@ public class PropertyProfileActivator } } + @Override + public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems ) + { + Activation activation = profile.getActivation(); + + if ( activation == null ) + { + return false; + } + + ActivationProperty property = activation.getProperty(); + + if ( property == null ) + { + return false; + } + return true; + } + } diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/building/ComplexActivationTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/building/ComplexActivationTest.java new file mode 100644 index 0000000000..9ef31b3db3 --- /dev/null +++ b/maven-model-builder/src/test/java/org/apache/maven/model/building/ComplexActivationTest.java @@ -0,0 +1,60 @@ +package org.apache.maven.model.building; + + /* + * 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. + */ + +import junit.framework.TestCase; + +import java.io.File; +import java.util.Properties; + +/** + * @author Konstantin Perikov + */ +public class ComplexActivationTest + extends TestCase +{ + + private File getPom( String name ) + { + return new File( "src/test/resources/poms/factory/" + name + ".xml" ).getAbsoluteFile(); + } + + public void testAndConditionInActivation() + throws Exception + { + Properties sysProperties = new Properties(); + sysProperties.setProperty( "myproperty", "test" ); + + ModelBuilder builder = new DefaultModelBuilderFactory().newInstance(); + assertNotNull( builder ); + + DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); + request.setProcessPlugins( true ); + request.setPomFile( getPom( "complex" ) ); + request.setSystemProperties( sysProperties ); + + ModelBuildingResult result = builder.build( request ); + assertNotNull( result ); + assertNotNull( result.getEffectiveModel() ); + assertEquals( "activated-1", result.getEffectiveModel().getProperties().get( "profile.file" ) ); + assertNull( result.getEffectiveModel().getProperties().get( "profile.miss" ) ); + } + +} diff --git a/maven-model-builder/src/test/resources/poms/factory/complex.xml b/maven-model-builder/src/test/resources/poms/factory/complex.xml new file mode 100644 index 0000000000..80060ffb43 --- /dev/null +++ b/maven-model-builder/src/test/resources/poms/factory/complex.xml @@ -0,0 +1,49 @@ + + + + 4.0.0 + + test + test + 0.1-SNAPSHOT + pom + + + hello + + + + + two-conditions + + + simple.xml + + + myproperty + test + + + + activated-1 + + + + another-two-conditions + + + myproperty + test + + + simple.xml + + + + activated-2 + + + +