Skip to content

Commit

Permalink
mojohaus#291 use scope/optional in profile over default
Browse files Browse the repository at this point in the history
If the scope/optional for a dependency with an entry in dependency
management is explicitly defined in a profile dependency but not in the
dependency management section, then use the scope/optional defined in
the profile dependency rather than falling back to the default value
  • Loading branch information
andpab authored and slawekjaranowski committed Aug 8, 2022
1 parent 5a86896 commit 374abca
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile_parent</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<javax.annotations.version>1.3.2</javax.annotations.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${javax.annotations.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>profile-with-deps-inherit-parent-depMgmt-jdk-optional-in-profile_parent</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>parent</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<configuration>
<embedBuildProfileDependencies>true</embedBuildProfileDependencies>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>java9</id>
<activation>
<jdk>[1.9,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>profile-with-deps-inherit-parent-depMgmt-jdk-scope-in-profile_parent</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<javax.annotations.version>1.3.2</javax.annotations.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${javax.annotations.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>profile-with-deps-inherit-parent-depMgmt-jdk-scope-in-profile</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.codehaus.mojo.flatten.its</groupId>
<artifactId>profile-with-deps-inherit-parent-depMgmt-jdk-scope-in-profile_parent</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>parent</relativePath>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<configuration>
<embedBuildProfileDependencies>true</embedBuildProfileDependencies>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>java9</id>
<activation>
<jdk>[1.9,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -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 'provided' == flattendProject.profiles.profile.dependencies.dependency.scope.text()
12 changes: 10 additions & 2 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -692,12 +692,20 @@ protected Model createCleanPom( Model effectivePom ) throws MojoExecutionExcepti
if ( managedDependencies.contains( parsedDep ) )
{
parsedDep.setVersion( managedDependencies.resolve( parsedDep ).getVersion() );
parsedDep.setScope( managedDependencies.resolve( parsedDep ).getScope() );
String managedDepScope = managedDependencies.resolve( parsedDep ).getScope();
if ( managedDepScope != null )
{
parsedDep.setScope( managedDepScope );
}
if ( parsedDep.getScope() == null )
{
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" );
Expand Down

0 comments on commit 374abca

Please sign in to comment.