Skip to content

Commit

Permalink
[MSHADE-413] Fix endless loop caused by manipulating shared objects (#…
Browse files Browse the repository at this point in the history
…124)

* Add IT
* [MSHADE-413] Fix endless loop caused by manipulating shared objects

Maven objects (like `Dependency` or `Model`) returned by Maven must not
be modified. Doing so will result in endless loops or incorrect builds.
  • Loading branch information
snazy committed Oct 21, 2022
1 parent 0945bcb commit 7078d92
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 26 deletions.
19 changes: 19 additions & 0 deletions src/it/projects/MSHADE-413-parallel/invoker.properties
@@ -0,0 +1,19 @@
# 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.

invoker.timeoutInSeconds=60
invoker.goals = clean install -T2 -pl p1/ -pl p2/
87 changes: 87 additions & 0 deletions src/it/projects/MSHADE-413-parallel/p1/pom.xml
@@ -0,0 +1,87 @@
<?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 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.its.shade.parallel</groupId>
<artifactId>mshade413-parent</artifactId>
<version>1.0</version>
</parent>

<artifactId>mshade413-p1</artifactId>
<version>1.0</version>

<name>MSHADE-413-p1</name>

<dependencies>
<dependency>
<groupId>org.projectnessie</groupId>
<artifactId>nessie-spark-extensions-base</artifactId>
<version>0.22.0</version>
</dependency>
<dependency>
<groupId>org.projectnessie</groupId>
<artifactId>nessie-spark-extensions-base</artifactId>
<version>0.22.0</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.iceberg</groupId>
<artifactId>iceberg-spark3-runtime</artifactId>
<version>0.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<artifactSet>
<includes>
<include>org.projectnessie</include>
</includes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
87 changes: 87 additions & 0 deletions src/it/projects/MSHADE-413-parallel/p2/pom.xml
@@ -0,0 +1,87 @@
<?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 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.its.shade.parallel</groupId>
<artifactId>mshade413-parent</artifactId>
<version>1.0</version>
</parent>

<artifactId>mshade413-p2</artifactId>
<version>1.0</version>

<name>MSHADE-413-p2</name>

<dependencies>
<dependency>
<groupId>org.projectnessie</groupId>
<artifactId>nessie-spark-extensions-base</artifactId>
<version>0.22.0</version>
</dependency>
<dependency>
<groupId>org.projectnessie</groupId>
<artifactId>nessie-spark-extensions-base</artifactId>
<version>0.22.0</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.iceberg</groupId>
<artifactId>iceberg-spark3-runtime</artifactId>
<version>0.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<artifactSet>
<includes>
<include>org.projectnessie</include>
</includes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
39 changes: 39 additions & 0 deletions src/it/projects/MSHADE-413-parallel/pom.xml
@@ -0,0 +1,39 @@
<?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 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.shade.parallel</groupId>
<artifactId>mshade413-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<name>MSHADE-413</name>
<description>
Test that shade works in two parallel project builds.
</description>

<modules>
<module>p1</module>
<module>p2</module>
</modules>
</project>
90 changes: 64 additions & 26 deletions src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
Expand Up @@ -1061,10 +1061,6 @@ private File shadedTestArtifactFile()
private void createDependencyReducedPom( Set<String> artifactsToRemove )
throws IOException, DependencyGraphBuilderException, ProjectBuildingException
{
List<Dependency> dependencies = new ArrayList<>();

boolean modified = false;

List<Dependency> transitiveDeps = new ArrayList<>();

// NOTE: By using the getArtifacts() we get the completely evaluated artifacts
Expand All @@ -1083,39 +1079,48 @@ private void createDependencyReducedPom( Set<String> artifactsToRemove )
// we'll figure out the exclusions in a bit.
transitiveDeps.add( dep );
}
List<Dependency> origDeps = project.getDependencies();

if ( promoteTransitiveDependencies )
Model model = project.getOriginalModel();

// MSHADE-413: Must not use objects (for example `Model` or `Dependency`) that are "owned
// by Maven" and being used by other projects/plugins. Modifying those will break the
// correctness of the build - or cause an endless loop.
List<Dependency> origDeps = new ArrayList<>();
List<Dependency> source = promoteTransitiveDependencies ? transitiveDeps : project.getDependencies();
for ( Dependency d : source )
{
origDeps = transitiveDeps;
origDeps.add( d.clone() );
}
model = model.clone();

Model model = project.getOriginalModel();
// MSHADE-185: We will remove all system scoped dependencies which usually
// have some kind of property usage. At this time the properties within
// such things are already evaluated.
List<Dependency> originalDependencies = model.getDependencies();
removeSystemScopedDependencies( artifactsToRemove, originalDependencies );

List<Dependency> dependencies = new ArrayList<>();
boolean modified = false;
for ( Dependency d : origDeps )
{
dependencies.add( d );

String id = getId( d );

if ( artifactsToRemove.contains( id ) )
if ( artifactsToRemove.contains( getId( d ) ) )
{
modified = true;

if ( keepDependenciesWithProvidedScope )
{
d.setScope( "provided" );
if ( !"provided".equals( d.getScope() ) )
{
modified = true;
d.setScope( "provided" );
}
}
else
{
dependencies.remove( d );
modified = true;
continue;
}
}

dependencies.add( d );
}

// MSHADE-155
Expand Down Expand Up @@ -1299,8 +1304,13 @@ public boolean updateExcludesInDeps( MavenProject project, List<Dependency> depe
boolean modified = false;
for ( DependencyNode n2 : node.getChildren() )
{
String artifactId2 = getId( n2.getArtifact() );

for ( DependencyNode n3 : n2.getChildren() )
{
Artifact artifact3 = n3.getArtifact();
String artifactId3 = getId( artifact3 );

// check if it really isn't in the list of original dependencies. Maven
// prior to 2.0.8 may grab versions from transients instead of
// from the direct deps in which case they would be marked included
Expand All @@ -1310,7 +1320,7 @@ public boolean updateExcludesInDeps( MavenProject project, List<Dependency> depe
boolean found = false;
for ( Dependency dep : transitiveDeps )
{
if ( getId( dep ).equals( getId( n3.getArtifact() ) ) )
if ( getId( dep ).equals( artifactId3 ) )
{
found = true;
break;
Expand All @@ -1321,18 +1331,31 @@ public boolean updateExcludesInDeps( MavenProject project, List<Dependency> depe
// note: MSHADE-31 introduced the exclusion logic for promoteTransitiveDependencies=true,
// but as of 3.2.1 promoteTransitiveDependencies has no effect for provided deps,
// which makes this fix even possible (see also MSHADE-181)
if ( !found && !"provided".equals( n3.getArtifact().getScope() ) )
if ( !found && !"provided".equals( artifact3.getScope() ) )
{
getLog().debug( String.format( "dependency %s (scope %s) not found in transitive dependencies",
artifactId3, artifact3.getScope() ) );
for ( Dependency dep : dependencies )
{
if ( getId( dep ).equals( getId( n2.getArtifact() ) ) )
if ( getId( dep ).equals( artifactId2 ) )
{
Exclusion exclusion = new Exclusion();
exclusion.setArtifactId( n3.getArtifact().getArtifactId() );
exclusion.setGroupId( n3.getArtifact().getGroupId() );
dep.addExclusion( exclusion );
modified = true;
break;
// MSHADE-413: First check whether the exclusion has already been added,
// because it's meaningless to add it more than once. Certain cases
// can end up adding the exclusion "forever" and cause an endless loop
// rewriting the whole dependency-reduced-pom.xml file.
if ( !dependencyHasExclusion( dep, artifact3 ) )
{
getLog().debug( String.format( "Adding exclusion for dependency %s (scope %s) "
+ "to %s (scope %s)",
artifactId3, artifact3.getScope(),
getId( dep ), dep.getScope() ) );
Exclusion exclusion = new Exclusion();
exclusion.setArtifactId( artifact3.getArtifactId() );
exclusion.setGroupId( artifact3.getGroupId() );
dep.addExclusion( exclusion );
modified = true;
break;
}
}
}
}
Expand All @@ -1347,6 +1370,21 @@ public boolean updateExcludesInDeps( MavenProject project, List<Dependency> depe
}
}

private boolean dependencyHasExclusion( Dependency dep, Artifact exclusionToCheck )
{
boolean containsExclusion = false;
for ( Exclusion existingExclusion : dep.getExclusions() )
{
if ( existingExclusion.getGroupId().equals( exclusionToCheck.getGroupId() )
&& existingExclusion.getArtifactId().equals( exclusionToCheck.getArtifactId() ) )
{
containsExclusion = true;
break;
}
}
return containsExclusion;
}

private List<ResourceTransformer> toResourceTransformers(
String shade, List<ResourceTransformer> resourceTransformers )
{
Expand Down

0 comments on commit 7078d92

Please sign in to comment.