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-36] Use reduced dependencies #25

Closed
Show file tree
Hide file tree
Changes from 2 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,18 @@
# 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.goals = clean package
95 changes: 95 additions & 0 deletions src/it/projects/MSHADE-36-inject-dep-reduced-pom-in-final/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.plugins.shade.its</groupId>
<artifactId>shade-parent</artifactId>
<version>1.0</version>
<relativePath>../setup-parent</relativePath>
</parent>

<groupId>org.apache.maven.its.shade.drp</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>MSHADE-36</name>
<description>
Test to see that the dependency-reduced-pom.xml is injected into the final jar instead of the original pom.xml
</description>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>attach-shade</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<createDependencyReducedPom>true</createDependencyReducedPom>
<useDependencyReducedPomInJar>true</useDependencyReducedPomInJar>
<relocations>
<relocation>
<pattern>org.codehaus.plexus</pattern>
<shadedPattern>com.example.shaded.org.codehaus.plexus</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<includes>
<include>junit:junit</include>
<include>org.codehaus.plexus:plexus-utils</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 com.example;

import org.codehaus.plexus.util.StringUtils;

public class Main
{
public static void main( String[] args ) {
System.out.println( "Hello World!" + ( isEmpty("foo") ? " is empty!" : " -- ") );
}

public static boolean isEmpty( String input ) {
return StringUtils.isEmpty( input );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.io.*;
import java.util.jar.*;
import java.util.Arrays;
import org.codehaus.plexus.util.*;

String[] wanted =
{
"com/example/Main.class",
"junit/swingui/icons/error.gif",
"com/example/shaded/org/codehaus/plexus/util/StringUtils.class",
};

String[] unwanted =
{
"junit/swingui/TestRunner.class",
michael-o marked this conversation as resolved.
Show resolved Hide resolved
"org/codehaus/plexus/util/StringUtils.class",
};

JarFile jarFile = null;
try
{
jarFile = new JarFile ( new File( basedir, "target/test-1.0.jar" ) );

for ( String path:wanted )
{
if ( jarFile.getEntry( path ) == null )
{
throw new IllegalStateException( "wanted path is missing: "+path );
}
}

for ( String path:unwanted )
{
if ( jarFile.getEntry( path ) != null )
{
throw new IllegalStateException ( "unwanted path is present: "+path );
}
}

JarEntry jarEntry = jarFile.getEntry( "META-INF/maven/org.apache.maven.its.shade.drp/test/pom.xml" );
String pomFile = IOUtil.toString( jarFile.getInputStream( jarEntry ), "UTF-8" );

if ( pomFile.contains( "<groupId>org.codehaus.plexus</groupId>" ) )
{
throw new IllegalStateException( "The pom.xml still contains a reference to the org.codehaus.plexus dependency" );
}

}
finally
{
if ( jarFile != null ) {
jarFile.close();
}
}
32 changes: 27 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 @@ -76,6 +76,7 @@
import java.util.Set;

import javax.inject.Inject;
import static org.apache.maven.plugins.shade.resource.UseDependencyReducedPom.createPomXmlReplaceTransformers;

/**
* Mojo that performs shading delegating to the Shader component.
Expand Down Expand Up @@ -285,6 +286,14 @@ public class ShadeMojo
@Parameter( defaultValue = "false" )
private boolean generateUniqueDependencyReducedPom;

/**
* Do we put the dependency reduced pom in the jar instead of the jar file provided by the project.
nielsbasjes marked this conversation as resolved.
Show resolved Hide resolved
*
* @since 3.3.0
*/
@Parameter( defaultValue = "false" )
private boolean useDependencyReducedPomInJar;

/**
* When true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed.
*/
Expand Down Expand Up @@ -449,6 +458,24 @@ public void execute()

List<ResourceTransformer> resourceTransformers = getResourceTransformers();

if ( createDependencyReducedPom )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have moved this before the Shader works. No changes in behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the tests present in the project today 2 fail with my changes in place.
So this does indeed need some rework.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently the problem that failed the tests made two files.
Fix that now all tests pass.

Still the question "No changes in behavior?" remains a valid one.

For now: Let's assume there IS a change the current tests did not pick up.
I'll look into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My verification steps:

  1. All integration tests pass.
  2. Checkout both master and my version into separate directories.
  3. Run in both: MAVEN_OPTS=-Dinvoker.parallelThreads=2 mvn clean verify -Prun-its under javac 11.0.11
  4. Get this tool to compare the jars https://github.com/scala/jardiff
  5. Run these magical incantations on my Linux machine:
    find target/it -type f -name '*.jar' | while read JAR ; do java -jar ../jardiff.jar ${JAR} ../maven-shade-plugin-master/${JAR} ; done > jar-changes.log
    find target/it -type f -name '*pom*' | while read POM ; do diff ${POM} ../maven-shade-plugin-master/${POM} ; done > pom-changes.log
  6. Manually go through the output.
    Differences logged:
  • Files like surefirebooter2116138492290729396.jar are unique (Ignore)
  • The test files I created are only present in mine (Ignore)
  • All files have a difference in the timestamps when the files were generated (pom.properties) (Ignore)
  • Some tests include the plugin itself and show differences because of the new feature (Ignore)

@michael-o Is this sufficient verification?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will verify!

{
createDependencyReducedPom( artifactIds );
}

if ( useDependencyReducedPomInJar )
nielsbasjes marked this conversation as resolved.
Show resolved Hide resolved
{
if ( !createDependencyReducedPom )
{
throw new MojoExecutionException(
"Cannot use the dependency-reduced-pom.xml if it is not created." );
}

// In some cases the used implementation of the resourceTransformers is immutable.
resourceTransformers = new ArrayList<>( resourceTransformers );
resourceTransformers.addAll( createPomXmlReplaceTransformers( project, dependencyReducedPomLocation ) );
}

ShadeRequest shadeRequest = shadeRequest( "jar", artifacts, outputJar, filters, relocators,
resourceTransformers );

Expand Down Expand Up @@ -588,11 +615,6 @@ else if ( !renamed )
}
}
}

if ( createDependencyReducedPom )
{
createDependencyReducedPom( artifactIds );
}
}
}
catch ( Exception e )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.apache.maven.plugins.shade.resource;

/*
* 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 org.apache.maven.project.MavenProject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* Manually creates the resource processors needed to remove the original pom.xml and inject
* the dependency-reduced-pom.xml in it's place in the shaded JAR.
nielsbasjes marked this conversation as resolved.
Show resolved Hide resolved
*/
public class UseDependencyReducedPom
{
public static List<ResourceTransformer> createPomXmlReplaceTransformers(
nielsbasjes marked this conversation as resolved.
Show resolved Hide resolved
MavenProject project,
File dependencyReducedPomLocation
)
{
String pomXmlInFinalJarFilename =
"META-INF/maven/" + project.getGroupId() + "/" + project.getArtifactId() + "/pom.xml";

List<ResourceTransformer> resourceTransformers = new ArrayList<>();

DontIncludeResourceTransformer removePomXML = new DontIncludeResourceTransformer();
removePomXML.resource = pomXmlInFinalJarFilename;
resourceTransformers.add( removePomXML );

IncludeResourceTransformer insertDependencyReducedPomXML = new IncludeResourceTransformer();
insertDependencyReducedPomXML.file = dependencyReducedPomLocation;
insertDependencyReducedPomXML.resource = pomXmlInFinalJarFilename;
resourceTransformers.add( insertDependencyReducedPomXML );

return resourceTransformers;
}
}