Skip to content

Commit 08d08a6

Browse files
committed
[MSHADE-36] Support for injecting the dependency-reduced-pom.xml into the final shaded jar
1 parent 57d78de commit 08d08a6

File tree

6 files changed

+306
-5
lines changed

6 files changed

+306
-5
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals = clean package
19+
invoker.java.version = 1.8+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<project>
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<parent>
26+
<groupId>org.apache.maven.plugins.shade.its</groupId>
27+
<artifactId>shade-parent</artifactId>
28+
<version>1.0</version>
29+
<relativePath>../setup-parent</relativePath>
30+
</parent>
31+
32+
<groupId>org.apache.maven.its.shade.drp</groupId>
33+
<artifactId>test</artifactId>
34+
<version>1.0</version>
35+
<packaging>jar</packaging>
36+
37+
<name>MSHADE-36</name>
38+
<description>
39+
Test to see that the dependency-reduced-pom.xml is injected into the final jar instead of the normal pom.xml
40+
</description>
41+
42+
<properties>
43+
<maven.compiler.source>1.4</maven.compiler.source>
44+
<maven.compiler.target>1.4</maven.compiler.target>
45+
</properties>
46+
47+
<dependencies>
48+
<dependency>
49+
<groupId>junit</groupId>
50+
<artifactId>junit</artifactId>
51+
<version>3.8.2</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.codehaus.plexus</groupId>
55+
<artifactId>plexus-utils</artifactId>
56+
<version>1.4.1</version>
57+
</dependency>
58+
</dependencies>
59+
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<version>2.0.2</version>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-shade-plugin</artifactId>
70+
<version>@project.version@</version>
71+
<executions>
72+
<execution>
73+
<id>attach-shade</id>
74+
<phase>package</phase>
75+
<goals>
76+
<goal>shade</goal>
77+
</goals>
78+
<configuration>
79+
<minimizeJar>true</minimizeJar>
80+
<createDependencyReducedPom>true</createDependencyReducedPom>
81+
<useDependencyReducedPomInJar>true</useDependencyReducedPomInJar>
82+
<relocations>
83+
<relocation>
84+
<pattern>org.codehaus.plexus</pattern>
85+
<shadedPattern>nl.example.shaded.org.codehaus.plexus</shadedPattern>
86+
</relocation>
87+
</relocations>
88+
<artifactSet>
89+
<includes>
90+
<include>junit:junit</include>
91+
<include>org.codehaus.plexus:plexus-utils</include>
92+
</includes>
93+
</artifactSet>
94+
</configuration>
95+
</execution>
96+
</executions>
97+
</plugin>
98+
</plugins>
99+
</build>
100+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package nl.example;
21+
22+
import org.codehaus.plexus.util.StringUtils;
23+
24+
public class Main
25+
{
26+
public static void main( String[] args ) {
27+
System.out.println( "Hello World!" + ( isEmpty("foo") ? " is empty!" : " -- ") );
28+
}
29+
30+
public static boolean isEmpty( String input ) {
31+
return StringUtils.isEmpty( input );
32+
}
33+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF ) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License" ); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import java.io.*;
21+
import java.util.jar.*;
22+
import java.util.Arrays;
23+
import org.codehaus.plexus.util.*;
24+
25+
String[] wanted =
26+
{
27+
"nl/example/Main.class",
28+
"junit/swingui/icons/error.gif",
29+
"nl/example/shaded/org/codehaus/plexus/util/StringUtils.class",
30+
};
31+
32+
String[] unwanted =
33+
{
34+
"junit/swingui/TestRunner.class",
35+
"org/codehaus/plexus/util/StringUtils.class",
36+
};
37+
38+
JarFile jarFile = null;
39+
try
40+
{
41+
jarFile = new JarFile( new File( basedir, "target/test-1.0.jar" ) );
42+
43+
for( String path:wanted )
44+
{
45+
if( jarFile.getEntry( path )==null )
46+
{
47+
throw new IllegalStateException( "wanted path is missing: "+path );
48+
}
49+
}
50+
51+
for( String path:unwanted )
52+
{
53+
if( jarFile.getEntry( path )!=null )
54+
{
55+
throw new IllegalStateException( "unwanted path is present: "+path );
56+
}
57+
}
58+
59+
JarEntry jarEntry=jarFile.getEntry( "META-INF/maven/org.apache.maven.its.shade.drp/test/pom.xml" );
60+
String pomFile=IOUtil.toString( jarFile.getInputStream( jarEntry ),"UTF-8" );
61+
62+
if( pomFile.contains( "<groupId>org.codehaus.plexus</groupId>" ) ){
63+
throw new IllegalStateException( "The pom.xml still contains a reference to the org.codehaus.plexus dependency" );
64+
}
65+
66+
}
67+
finally
68+
{
69+
if ( jarFile != null ) {
70+
jarFile.close();
71+
}
72+
}

src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
import java.util.Map;
8181
import java.util.Set;
8282

83+
import static org.apache.maven.plugins.shade.resource.UseDependencyReducedPomXml.createPomXmlReplaceTransformers;
84+
8385
/**
8486
* Mojo that performs shading delegating to the Shader component.
8587
*
@@ -289,6 +291,14 @@ public class ShadeMojo
289291
@Parameter( defaultValue = "false" )
290292
private boolean generateUniqueDependencyReducedPom;
291293

294+
/**
295+
* Do we put the dependency reduced pom in the jar instead of the jar file provided by the project.
296+
*
297+
* @since 3.2.2
298+
*/
299+
@Parameter( defaultValue = "false" )
300+
private boolean useDependencyReducedPomInJar;
301+
292302
/**
293303
* When true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed.
294304
*/
@@ -437,6 +447,24 @@ public void execute()
437447

438448
List<ResourceTransformer> resourceTransformers = getResourceTransformers();
439449

450+
if ( createDependencyReducedPom )
451+
{
452+
createDependencyReducedPom( artifactIds );
453+
}
454+
455+
if ( useDependencyReducedPomInJar )
456+
{
457+
if ( !createDependencyReducedPom )
458+
{
459+
throw new MojoExecutionException(
460+
"Cannot use the dependency-reduced-pom.xml if it is not created." );
461+
}
462+
463+
// In some cases the used implementation of the resourceTransformers is immutable.
464+
resourceTransformers = new ArrayList<>( resourceTransformers );
465+
resourceTransformers.addAll( createPomXmlReplaceTransformers( project, dependencyReducedPomLocation ) );
466+
}
467+
440468
ShadeRequest shadeRequest = shadeRequest( artifacts, outputJar, filters, relocators, resourceTransformers );
441469

442470
shader.shade( shadeRequest );
@@ -532,11 +560,6 @@ else if ( !renamed )
532560

533561
projectHelper.attachArtifact( project, "jar", "tests", shadedTests );
534562
}
535-
536-
if ( createDependencyReducedPom )
537-
{
538-
createDependencyReducedPom( artifactIds );
539-
}
540563
}
541564
}
542565
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.apache.maven.plugins.shade.resource;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.apache.maven.project.MavenProject;
23+
24+
import java.io.File;
25+
import java.util.Arrays;
26+
import java.util.List;
27+
28+
/**
29+
* Manually creates the resource processors needed to remove the original pom.xml and inject
30+
* the dependency-reduced-pom.xml in it's place in the shaded JAR.
31+
*/
32+
public class UseDependencyReducedPomXml
33+
{
34+
public static List<ResourceTransformer> createPomXmlReplaceTransformers(
35+
MavenProject project,
36+
File dependencyReducedPomLocation
37+
)
38+
{
39+
String pomXmlInFinalJarFilename =
40+
"META-INF/maven/" + project.getGroupId() + "/" + project.getArtifactId() + "/pom.xml";
41+
42+
DontIncludeResourceTransformer removePomXML = new DontIncludeResourceTransformer();
43+
removePomXML.resource = pomXmlInFinalJarFilename;
44+
45+
IncludeResourceTransformer insertDependencyReducedPomXML = new IncludeResourceTransformer();
46+
insertDependencyReducedPomXML.file = dependencyReducedPomLocation;
47+
insertDependencyReducedPomXML.resource = pomXmlInFinalJarFilename;
48+
49+
return Arrays.asList(
50+
removePomXML,
51+
insertDependencyReducedPomXML
52+
);
53+
}
54+
}

0 commit comments

Comments
 (0)