Author: sseifert Date: Tue Jul 14 11:39:49 2015 New Revision: 1690902 URL: http://svn.apache.org/r1690902 Log: SLING-4880 apply resolved dependencies to raw model instead of attaching effective model add new optional parameter "allowUnresolvedPomDependencies" (default: false)
Added: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyArtifactVersionsTest.java (with props) Modified: sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/AbstractSlingStartMojo.java sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/AttachSlingStartModel.java sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolver.java sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/PreparePackageMojo.java sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelResolveUtility.java sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelProcessorTest.java sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java Modified: sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/AbstractSlingStartMojo.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/AbstractSlingStartMojo.java?rev=1690902&r1=1690901&r2=1690902&view=diff ============================================================================== --- sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/AbstractSlingStartMojo.java (original) +++ sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/AbstractSlingStartMojo.java Tue Jul 14 11:39:49 2015 @@ -60,6 +60,13 @@ public abstract class AbstractSlingStart @Parameter(defaultValue="false") protected boolean usePomDependencies; + /** + * If set to true, an exception is throws when "usePomDependencies" is set to true and some + * dependency version could not be resolved in the Maven POM. + */ + @Parameter(defaultValue="false") + protected boolean allowUnresolvedPomDependencies; + protected File getTmpDir() { return new File(this.project.getBuild().getDirectory(), "slingstart-tmp"); } Modified: sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/AttachSlingStartModel.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/AttachSlingStartModel.java?rev=1690902&r1=1690901&r2=1690902&view=diff ============================================================================== --- sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/AttachSlingStartModel.java (original) +++ sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/AttachSlingStartModel.java Tue Jul 14 11:39:49 2015 @@ -49,7 +49,7 @@ public class AttachSlingStartModel exten model = ModelUtility.applyVariables(model, new PomVariableResolver(project)); } if (usePomDependencies) { - // TODO: implement applyDependencies + model = ModelUtility.applyArtifactVersions(model, new PomArtifactVersionResolver(project, allowUnresolvedPomDependencies)); } // write the model Modified: sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java?rev=1690902&r1=1690901&r2=1690902&view=diff ============================================================================== --- sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java (original) +++ sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/DependencyLifecycleParticipant.java Tue Jul 14 11:39:49 2015 @@ -152,7 +152,8 @@ public class DependencyLifecycleParticip resolverOptions.variableResolver(new PomVariableResolver(info.project)); } if (nodeBooleanValue(info.plugin, "usePomDependencies", false)) { - resolverOptions.artifactVersionResolver(new PomArtifactVersionResolver(info.project)); + resolverOptions.artifactVersionResolver(new PomArtifactVersionResolver(info.project, + nodeBooleanValue(info.plugin, "allowUnresolvedPomDependencies", false))); } // we have to create an effective model to add the dependencies Modified: sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolver.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolver.java?rev=1690902&r1=1690901&r2=1690902&view=diff ============================================================================== --- sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolver.java (original) +++ sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/PomArtifactVersionResolver.java Tue Jul 14 11:39:49 2015 @@ -33,12 +33,15 @@ import org.apache.sling.provisioning.mod public class PomArtifactVersionResolver implements ArtifactVersionResolver { private final MavenProject project; + private final boolean allowUnresolvedPomDependencies; /** * @param project Maven project + * @param allowUnresolvedPomDependencies If true, no exception is thrown when resolving is not possible */ - public PomArtifactVersionResolver(MavenProject project) { + public PomArtifactVersionResolver(MavenProject project, boolean allowUnresolvedPomDependencies) { this.project = project; + this.allowUnresolvedPomDependencies = allowUnresolvedPomDependencies; } @Override @@ -53,7 +56,12 @@ public class PomArtifactVersionResolver return version; } } - return null; + if (allowUnresolvedPomDependencies) { + return null; + } + else { + throw new IllegalArgumentException("Unable to resolve dependency: " + artifact.toMvnUrl()); + } } private String findVersion(List<Dependency> dependencies, Artifact artifact) { Modified: sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/PreparePackageMojo.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/PreparePackageMojo.java?rev=1690902&r1=1690901&r2=1690902&view=diff ============================================================================== --- sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/PreparePackageMojo.java (original) +++ sling/trunk/tooling/maven/slingstart-maven-plugin/src/main/java/org/apache/sling/maven/slingstart/PreparePackageMojo.java Tue Jul 14 11:39:49 2015 @@ -108,7 +108,7 @@ public class PreparePackageMojo extends options.variableResolver(new PomVariableResolver(project)); } if (usePomDependencies) { - options.artifactVersionResolver(new PomArtifactVersionResolver(project)); + options.artifactVersionResolver(new PomArtifactVersionResolver(project, allowUnresolvedPomDependencies)); } return options; } Modified: sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelResolveUtility.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelResolveUtility.java?rev=1690902&r1=1690901&r2=1690902&view=diff ============================================================================== --- sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelResolveUtility.java (original) +++ sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelResolveUtility.java Tue Jul 14 11:39:49 2015 @@ -90,7 +90,7 @@ class ModelResolveUtility { */ static String resolveArtifactVersion(final String groupId, final String artifactId, final String version, final String classifier, final String type, ArtifactVersionResolver artifactVersionResolver) { - if (artifactVersionResolver != null && "LATEST".equals(version)) { + if (artifactVersionResolver != null && (version == null || "LATEST".equals(version))) { String newVersion = artifactVersionResolver.resolve(new Artifact(groupId, artifactId, version, classifier, type)); if (newVersion != null) { return newVersion; Modified: sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java?rev=1690902&r1=1690901&r2=1690902&view=diff ============================================================================== --- sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java (original) +++ sling/trunk/tooling/support/provisioning-model/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java Tue Jul 14 11:39:49 2015 @@ -17,6 +17,7 @@ package org.apache.sling.provisioning.model; import static org.apache.sling.provisioning.model.ModelResolveUtility.getProcessedConfiguration; +import static org.apache.sling.provisioning.model.ModelResolveUtility.resolveArtifactVersion; import java.util.Arrays; import java.util.Enumeration; @@ -422,19 +423,19 @@ public abstract class ModelUtility { * The variable resolver may look up variables on it's own, or fallback to the variables already defined for the feature. * All resolved variable values are collected and put to the "variables" section of the resulting model. * @param model Original model - * @param variableResolver Variable resolver + * @param resolver Variable resolver * @return Model with updated "variables" section. * @throws IllegalArgumentException If a variable can't be replaced or configuration properties can't be parsed * @since 1.3 */ - public static Model applyVariables(final Model model, final VariableResolver variableResolver) { + public static Model applyVariables(final Model model, final VariableResolver resolver) { // define delegating resolver that collects all variable names and value per feature final Map<String,Map<String,String>> collectedVars = new HashMap<String, Map<String,String>>(); VariableResolver variableCollector = new VariableResolver() { @Override public String resolve(Feature feature, String name) { - String value = variableResolver.resolve(feature, name); + String value = resolver.resolve(feature, name); if (value != null) { Map<String,String> featureVars = collectedVars.get(feature.getName()); if (featureVars == null) { @@ -469,4 +470,39 @@ public abstract class ModelUtility { return variablesUpdater.process(model); } + /** + * Resolves artifact versions that are no set explicitly in the provisioning file via the given resolver (version = "LATEST"). + * If the resolver does not resolve to a version "LATEST" is left in the model. + * The resolver may decide to raise an IllegalArgumentException in this case if unresolved dependencies are no allowed. + * @param model Original model + * @param resolver Artifact version resolver + * @return Model with updated artifact versions + * @throws IllegalArgumentException If the provider does not allow unresolved version and a version could not be resolved + * @since 1.3 + */ + public static Model applyArtifactVersions(final Model model, final ArtifactVersionResolver resolver) { + + // define a processor that updates the versions of artifacts + ModelProcessor versionUpdater = new ModelProcessor() { + @Override + protected Artifact processArtifact(Artifact artifact, Feature newFeature, RunMode newRunMode) { + String newVersion = resolveArtifactVersion( + artifact.getGroupId(), + artifact.getArtifactId(), + artifact.getVersion(), + artifact.getClassifier(), + artifact.getType(), + resolver); + return new Artifact(artifact.getGroupId(), + artifact.getArtifactId(), + newVersion, + artifact.getClassifier(), + artifact.getType()); + } + }; + + // return model with updated version artifacts + return versionUpdater.process(model); + } + } Modified: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelProcessorTest.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelProcessorTest.java?rev=1690902&r1=1690901&r2=1690902&view=diff ============================================================================== --- sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelProcessorTest.java (original) +++ sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelProcessorTest.java Tue Jul 14 11:39:49 2015 @@ -19,11 +19,9 @@ package org.apache.sling.provisioning.model; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import java.util.Enumeration; -import java.util.Iterator; import java.util.Map.Entry; import org.junit.Before; @@ -114,20 +112,9 @@ public class ModelProcessorTest { assertEquals("LocRMG11", group12.getLocation()); assertEquals("ComRMG11", group12.getComment()); - Iterator<Artifact> artifacts = group12.iterator(); - Artifact artifact1 = artifacts.next(); - assertEquals("#g1", artifact1.getGroupId()); - assertEquals("#a1", artifact1.getArtifactId()); - assertEquals("#v1", artifact1.getVersion()); - assertEquals("#c1", artifact1.getClassifier()); - assertEquals("#t1", artifact1.getType()); - - Artifact artifact2 = artifacts.next(); - assertEquals("#g2", artifact2.getGroupId()); - assertEquals("#a2", artifact2.getArtifactId()); - assertEquals("#v2", artifact2.getVersion()); - - assertFalse(artifacts.hasNext()); + U.assertArtifactsInGroup(group12, 2); + U.assertArtifact(group12, "mvn:#g1/#a1/#v1/#t1/#c1"); + U.assertArtifact(group12, "mvn:#g2/#a2/#v2/#jar"); assertEquals("LocConf12", runMode12.getConfigurations().getLocation()); assertEquals("ComConf12", runMode12.getConfigurations().getComment()); @@ -157,11 +144,8 @@ public class ModelProcessorTest { ArtifactGroup group21 = runMode21.getArtifactGroup(20); assertNotNull(group21); - artifacts = group21.iterator(); - Artifact artifact3 = artifacts.next(); - assertEquals("#g3", artifact3.getGroupId()); - assertEquals("#a3", artifact3.getArtifactId()); - assertFalse(artifacts.hasNext()); + U.assertArtifactsInGroup(group21, 1); + U.assertArtifact(group21, "mvn:#g3/#a3/#LATEST/#jar"); } @@ -179,11 +163,11 @@ public class ModelProcessorTest { @Override protected Artifact processArtifact(Artifact artifact, Feature feature, RunMode runMode) { Artifact newArtifact = new Artifact( - "#" + artifact.getGroupId(), - "#" + artifact.getArtifactId(), - "#" + artifact.getVersion(), - "#" + artifact.getClassifier(), - "#" + artifact.getType()); + artifact.getGroupId()!=null ? "#" + artifact.getGroupId() : null, + artifact.getArtifactId()!=null ? "#" + artifact.getArtifactId() : null, + artifact.getVersion()!=null ? "#" + artifact.getVersion() : "#LATEST", + artifact.getClassifier()!=null ? "#" + artifact.getClassifier() : null, + artifact.getType()!=null ? "#" + artifact.getType() : null); return newArtifact; } Added: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyArtifactVersionsTest.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyArtifactVersionsTest.java?rev=1690902&view=auto ============================================================================== --- sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyArtifactVersionsTest.java (added) +++ sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyArtifactVersionsTest.java Tue Jul 14 11:39:49 2015 @@ -0,0 +1,76 @@ +/* + * 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. + */ +package org.apache.sling.provisioning.model; + +import org.apache.sling.provisioning.model.ModelUtility.ArtifactVersionResolver; +import org.junit.Before; +import org.junit.Test; + +public class ModelUtilityApplyArtifactVersionsTest { + + private Model testModel; + private ArtifactVersionResolver testArtifactVersionResolver; + + @Before + public void setUp() { + testModel = new Model(); + + Feature feature = testModel.getOrCreateFeature("feature1"); + RunMode runMode = feature.getOrCreateRunMode(new String[] { "rm1"}); + ArtifactGroup group = runMode.getOrCreateArtifactGroup(10); + + group.add(new Artifact("g1", "a1", "v1", "c1", "t1")); + group.add(new Artifact("g2", "a2", "LATEST", null, null)); + group.add(new Artifact("g3", "a3", "LATEST", null, null)); + + // dummy variable resolver that simulates external resolving of some variables + testArtifactVersionResolver = new ArtifactVersionResolver() { + @Override + public String resolve(Artifact artifact) { + if ("g2".equals(artifact.getGroupId()) && "a2".equals(artifact.getArtifactId())) { + return "v2"; + } + return null; + } + }; + } + + @Test + public void testApplyArtifactVersions() { + Model model = ModelUtility.applyArtifactVersions(testModel, testArtifactVersionResolver); + + Feature feature = model.getFeature("feature1"); + RunMode runMode = feature.getRunMode("rm1"); + ArtifactGroup group = runMode.getArtifactGroup(10); + + U.assertArtifactsInGroup(group, 3); + U.assertArtifact(group, "mvn:g1/a1/v1/t1/c1"); + U.assertArtifact(group, "mvn:g2/a2/v2/jar"); + U.assertArtifact(group, "mvn:g3/a3//jar"); + } + + @Test(expected=IllegalArgumentException.class) + public void testApplyVariablesInvalidVariable() { + ModelUtility.applyArtifactVersions(testModel, new ArtifactVersionResolver() { + @Override + public String resolve(Artifact artifact) { + throw new IllegalArgumentException("Unable to resolve."); + } + }); + } + +} Propchange: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyArtifactVersionsTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyArtifactVersionsTest.java ------------------------------------------------------------------------------ --- svn:keywords (added) +++ svn:keywords Tue Jul 14 11:39:49 2015 @@ -0,0 +1 @@ +LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author Propchange: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyArtifactVersionsTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java?rev=1690902&r1=1690901&r2=1690902&view=diff ============================================================================== --- sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java (original) +++ sling/trunk/tooling/support/provisioning-model/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java Tue Jul 14 11:39:49 2015 @@ -79,8 +79,9 @@ public class ModelUtilityApplyVariablesT RunMode runMode = effectiveFeature.getRunMode("rm1"); ArtifactGroup group = runMode.getArtifactGroup(10); - group.add(new Artifact("g1", "a1", "${param1}", "c1", "t1")); - group.add(new Artifact("g2", "a2", "${extparam2}", null, null)); + U.assertArtifactsInGroup(group, 2); + U.assertArtifact(group, "mvn:g1/a1/v1/t1/c1"); + U.assertArtifact(group, "mvn:g2/a2/extvalue2/jar"); Configuration conf = runMode.getConfiguration("pid1", null); assertEquals("extvalue1", conf.getProperties().get("conf1"));