From 8b4365d98979e45d93cb00d6ddf5e3844aa8dc2d Mon Sep 17 00:00:00 2001 From: "Pabst, Andreas" Date: Sat, 6 Aug 2022 14:24:10 +0200 Subject: [PATCH] #291 use optional of profile dep over default If the optional for a dependency with an entry in dependency management is defined in a profile dependency but not in the dependency management section, then use the optional defined in the profile dependency rather than falling back to the default value "false" --- .../parent/pom.xml | 23 ++++++++++ .../pom.xml | 42 +++++++++++++++++++ .../verify.groovy | 35 ++++++++++++++++ .../codehaus/mojo/flatten/FlattenMojo.java | 6 ++- 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/parent/pom.xml create mode 100644 src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/pom.xml create mode 100644 src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/verify.groovy diff --git a/src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/parent/pom.xml b/src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/parent/pom.xml new file mode 100644 index 00000000..eae41161 --- /dev/null +++ b/src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/parent/pom.xml @@ -0,0 +1,23 @@ + + 4.0.0 + org.codehaus.mojo.flatten.its + profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile_parent + 0.1-SNAPSHOT + pom + + + 1.3.2 + + + + + + javax.annotation + javax.annotation-api + ${javax.annotations.version} + + + + diff --git a/src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/pom.xml b/src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/pom.xml new file mode 100644 index 00000000..9d93fc3b --- /dev/null +++ b/src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + org.codehaus.mojo.flatten.its + profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile + 0.1-SNAPSHOT + jar + + org.codehaus.mojo.flatten.its + profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile_parent + 0.1-SNAPSHOT + parent + + + + + org.codehaus.mojo + flatten-maven-plugin + + true + + + + + + + + java9 + + [1.9,) + + + + javax.annotation + javax.annotation-api + true + + + + + diff --git a/src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/verify.groovy b/src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/verify.groovy new file mode 100644 index 00000000..621d44a9 --- /dev/null +++ b/src/it/projects/profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile/verify.groovy @@ -0,0 +1,35 @@ +/* + * 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. + */ +File originalPom = new File( basedir, 'pom.xml' ) +assert originalPom.exists() + +def originalProject = new XmlSlurper().parse( originalPom ) +assert 0 == originalProject.dependencies.size() +assert 'java9' == originalProject.profiles.profile.id.text() +assert 1 == originalProject.profiles.profile.dependencies.size() + +File flattendPom = new File( basedir, '.flattened-pom.xml' ) +assert flattendPom.exists() + +def flattendProject = new XmlSlurper().parse( flattendPom ) +assert 0 == flattendProject.dependencies.size() +assert 1 == flattendProject.profiles.profile.dependencies.dependency.size() +assert 'javax.annotation-api' == flattendProject.profiles.profile.dependencies.dependency.artifactId.text() +assert '1.3.2' == flattendProject.profiles.profile.dependencies.dependency.version.text() +assert 'true' == flattendProject.profiles.profile.dependencies.dependency.optional.text() diff --git a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java index aa416380..f41653ff 100644 --- a/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java +++ b/src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java @@ -701,7 +701,11 @@ protected Model createCleanPom( Model effectivePom ) throws MojoExecutionExcepti { parsedDep.setScope( "compile" ); } - parsedDep.setOptional( managedDependencies.resolve( parsedDep ).getOptional() ); + String managedDepOptional = managedDependencies.resolve( parsedDep ).getOptional(); + if ( managedDepOptional != null ) + { + parsedDep.setOptional( managedDepOptional ); + } if ( parsedDep.getOptional() == null ) { parsedDep.setOptional( "false" );