Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHADE-419] createDependencyReducedPom should not replace original pom when shadedArtifactAttached #149

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven.its.shade.cdrp-mm</groupId>
<artifactId>mshade-419-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>mshade-419-api</artifactId>
<version>1.0</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.
*/

public class Api
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven.its.shade.cdrp-mm</groupId>
<artifactId>mshade-419-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>mshade-419-simple-user</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<!-- MSHADE-419:
The mshade-419-impl project attaches a shaded jar, which we do not use here.
Thus, we would expect transitive dependencies to still get pulled in.
However, the compilation of this project fails with:

SimpleUser.java:[23,5] cannot find symbol
symbol: class Api

Note that both the normal and the shaded jar of mshade-419-impl are created with
the correct pom.
The missing transitive dependencies only manifest themselves in a multimodule build.
-->
<groupId>org.apache.maven.its.shade.cdrp-mm</groupId>
<artifactId>mshade-419-impl</artifactId>
<version>1.0</version>
<type>jar</type>
<!-- uncommenting the following avoids the MSHADE-419 problem -->
<!-- <classifier>shaded</classifier> -->
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/

public class SimpleUser
{
Impl impl = new Impl();
Api api = new Impl();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven.its.shade.cdrp-mm</groupId>
<artifactId>mshade-419-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>mshade-419-impl</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.its.shade.cdrp-mm</groupId>
<artifactId>mshade-419-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<!-- uncommenting the following avoids the MSHADE-419 problem -->
<!-- <createDependencyReducedPom>false</createDependencyReducedPom> -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.
*/

public class Impl extends Api
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its.shade.cdrp-mm</groupId>
<artifactId>mshade-419-parent</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>api</module>
<module>impl</module>
<module>impl-user</module>
</modules>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 java.util.jar.*
import org.codehaus.plexus.util.*

// note that the following checks were passing even before any fix for MSHADE-419,
// since the generated poms and jars are generated correctly.
// only compilation of the impl-user module is failing because it uses the normal jar but
// without its transitive dependencies

String jarPrefix = "impl/target/mshade-419-impl-1.0"
String jarPomPath = "META-INF/maven/org.apache.maven.its.shade.cdrp-mm/mshade-419-impl/pom.xml"
String apiDependencyString = "<artifactId>mshade-419-api</artifactId>"

String mainPomFileContent = FileUtils.fileRead( new File( basedir, "impl/pom.xml" ), "UTF-8" )
if ( !mainPomFileContent.contains( apiDependencyString ) )
{
throw new IllegalStateException( "The pom.xml should still contain the api dependency:\n" + mainPomFileContent )
}

String reducedPomFileContent = FileUtils.fileRead( new File( basedir, "impl/dependency-reduced-pom.xml" ), "UTF-8" )
if ( reducedPomFileContent.contains( apiDependencyString ) )
{
throw new IllegalStateException( "The dependency-reduced-pom.xml should not contain the api dependency:\n" + reducedPomFileContent )
}

JarFile jarFile = null
try
{
jarFile = new JarFile ( new File( basedir, jarPrefix + ".jar" ) )
JarEntry jarEntry = jarFile.getEntry(jarPomPath )
String pomFile = IOUtil.toString( jarFile.getInputStream( jarEntry ), "UTF-8" )
if ( !pomFile.contains( apiDependencyString ) )
{
throw new IllegalStateException( "The pom.xml in the normal jar should still contain the api dependency:\n" + pomFile )
}
}
finally
{
if ( jarFile != null )
{
jarFile.close()
}
}
22 changes: 17 additions & 5 deletions src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,10 @@ public void execute()

if ( createDependencyReducedPom )
{
createDependencyReducedPom( artifactIds );
// MSHADE-419: only replace the pom of this project with the dependency reduced one,
// when the shaded artifact is replacing the original artifact
boolean modifyCurrentProjectModel = !shadedArtifactAttached;
createDependencyReducedPom( artifactIds, modifyCurrentProjectModel );

if ( useDependencyReducedPomInJar )
{
Expand Down Expand Up @@ -1058,7 +1061,7 @@ private File shadedTestArtifactFile()

// We need to find the direct dependencies that have been included in the uber JAR so that we can modify the
// POM accordingly.
private void createDependencyReducedPom( Set<String> artifactsToRemove )
private void createDependencyReducedPom( Set<String> artifactsToRemove, boolean modifyCurrentProjectModel )
throws IOException, DependencyGraphBuilderException, ProjectBuildingException
{
List<Dependency> dependencies = new ArrayList<>();
Expand Down Expand Up @@ -1091,6 +1094,10 @@ private void createDependencyReducedPom( Set<String> artifactsToRemove )
}

Model model = project.getOriginalModel();
if ( !modifyCurrentProjectModel )
{
model = model.clone();
}
// 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.
Expand Down Expand Up @@ -1127,11 +1134,13 @@ private void createDependencyReducedPom( Set<String> artifactsToRemove )
addSystemScopedDependencyFromNonInterpolatedPom( dependencies, originalDependencies );

// Check to see if we have a reduction and if so rewrite the POM.
rewriteDependencyReducedPomIfWeHaveReduction( dependencies, modified, transitiveDeps, model );
rewriteDependencyReducedPomIfWeHaveReduction( dependencies, modified, transitiveDeps, model,
modifyCurrentProjectModel );
}

private void rewriteDependencyReducedPomIfWeHaveReduction( List<Dependency> dependencies, boolean modified,
List<Dependency> transitiveDeps, Model model )
List<Dependency> transitiveDeps, Model model,
boolean modifyCurrentProjectModel )
throws IOException, ProjectBuildingException,
DependencyGraphBuilderException
{
Expand Down Expand Up @@ -1220,7 +1229,10 @@ private void rewriteDependencyReducedPomIfWeHaveReduction( List<Dependency> depe
modified = updateExcludesInDeps( result.getProject(), dependencies, transitiveDeps );
}

project.setFile( dependencyReducedPomLocation );
if ( modifyCurrentProjectModel )
{
project.setFile( dependencyReducedPomLocation );
}
}
}

Expand Down