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-373: adds a property to skip manifest transformer application when transforming a source file. #59

Merged
merged 3 commits into from
Jul 4, 2020
Merged
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ For changes of a trivial nature to comments and documentation, it is not always
necessary to create a new ticket in JIRA. In this case, it is appropriate to
start the first line of a commit with '(doc)' instead of a ticket number.

Developer Tips
--------------

If you machine is big enough and you want to parallelise the IT execution to validate the build
before a PR you can set the concurrency in MAVEN_OPTS:

````
MAVEN_OPTS=-Dinvoker.parallelThreads=2 mvn verify -Prun-its
````

You can also run a single IT test using:

````
mvn verify -Prun-its -Dinvoker.test=myitproject
````

Additional Resources
--------------------

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
<goals>
<goal>package</goal>
</goals>
<showErrors>true</showErrors>
<projectsDirectory>src/it/projects</projectsDirectory>
<settingsFile>src/it/mrm/settings.xml</settingsFile>
<extraArtifacts>
Expand Down
4 changes: 2 additions & 2 deletions src/it/projects/MSHADE-183/verify.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if ( !"org.apache.maven.Shade".equals( mf.getMainAttributes().getValue( "Main-Cl
{
throw new IllegalStateException( "META-INF/MANIFEST.MF is incomplete" );
}
if ( !"null".equals( mf.getMainAttributes().getValue( "Implementation-Build" ) ) )
if ( null != mf.getMainAttributes().getValue( "Implementation-Build" ) )
{
throw new IllegalStateException( "META-INF/MANIFEST.MF Implementation-Build content is not null as expected." );
throw new IllegalStateException( "META-INF/MANIFEST.MF Implementation-Build content is not null as expected. (" + mf.getMainAttributes().entrySet() + ")" );
}
117 changes: 117 additions & 0 deletions src/it/projects/MSHADE-373/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?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.mt</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>MSHADE-373</name>
<description>
Test that reproduces the issue described in MSHADE-373.
</description>

<dependencies>
<dependency>
<!-- dummy dependency to test interaction of multiple manifests -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>@project.version@</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-install-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<mainClass>org.apache.maven.Main</mainClass>
</manifest>
<manifestEntries>
<Test-Entry>PASSED</Test-Entry>
<Original-Entry>PASSED</Original-Entry>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.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>
<createSourcesJar>true</createSourcesJar>
<shadeTestJar>true</shadeTestJar>
<shadedArtifactAttached>false</shadedArtifactAttached>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Custom-Entry>SET</Custom-Entry> <!-- not using original manifest but using transformer directly -->
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<forShade>sources-jar</forShade>
<manifestEntries>
<Test-Entry>FAILED</Test-Entry>
<Archiver-Version />
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.3.1</version>
</plugin>
</plugins>
</build>
</project>
62 changes: 62 additions & 0 deletions src/it/projects/MSHADE-373/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.*;

// NOTE: We deliberately use JarInputStream and not JarFile here!
Manifest extractManifest(name) {
file = new File( basedir, "target/" + name );
if ( !file.exists() )
{
throw new IllegalStateException( "No file '" + file + "'" );
}
JarInputStream jarStream = new JarInputStream( new FileInputStream( file ) );
Manifest mf = jarStream.getManifest();
jarStream.close();
return mf;
}

void assertsEntries( name, expectedTestEntry, expectedOriginalEntry, expectedCustomEntry, shouldHaveArchiverVersion ) {
attributes = extractManifest( name ).getMainAttributes();

if ( !Objects.equals( expectedTestEntry, attributes.getValue( "Test-Entry" ) ) )
{
throw new IllegalStateException( "Test-Entry should have been '" + expectedTestEntry + "' in " + name + ": " + attributes.entrySet() );
}

if ( !Objects.equals( expectedOriginalEntry, attributes.getValue( "Original-Entry" ) ) )
{
throw new IllegalStateException( "Original-Entry should have been '" + expectedOriginalEntry + "' in " + name + ": " + attributes.entrySet() );
}

if ( !Objects.equals( expectedCustomEntry, attributes.getValue( "Custom-Entry" ) ) )
{
throw new IllegalStateException( "Custom-Entry should have been '" + expectedCustomEntry + "' in " + name + ": " + attributes.entrySet() );
}

if ( shouldHaveArchiverVersion && attributes.getValue( "Archiver-Version" ) == null ||
!shouldHaveArchiverVersion && attributes.getValue( "Archiver-Version" ) != null )
{
throw new IllegalStateException( "META-INF/MANIFEST.mf Archiver-Version entry is invalid in " + name + ": " + attributes.entrySet() );
}
}

assertsEntries( "test-1.0.jar", "PASSED", "PASSED", "SET", true );
assertsEntries( "test-1.0-sources.jar", "FAILED", null, null, false ); // specific config
assertsEntries( "test-1.0-tests.jar", "PASSED", "PASSED", "SET", true ); // inherits from the default transformer
60 changes: 50 additions & 10 deletions src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.maven.plugins.shade.pom.PomWriter;
import org.apache.maven.plugins.shade.relocation.Relocator;
import org.apache.maven.plugins.shade.relocation.SimpleRelocator;
import org.apache.maven.plugins.shade.resource.ManifestResourceTransformer;
import org.apache.maven.plugins.shade.resource.ResourceTransformer;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -458,31 +459,33 @@ public void execute()

List<ResourceTransformer> resourceTransformers = getResourceTransformers();

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

shader.shade( shadeRequest );

if ( createSourcesJar )
{
ShadeRequest shadeSourcesRequest =
createShadeSourcesRequest( sourceArtifacts, sourcesJar, filters, relocators, resourceTransformers );
createShadeSourcesRequest( "sources-jar", sourceArtifacts, sourcesJar, filters, relocators,
resourceTransformers );

shader.shade( shadeSourcesRequest );
}

if ( shadeTestJar )
{
ShadeRequest shadeTestRequest =
shadeRequest( testArtifacts, testJar, filters, relocators, resourceTransformers );
shadeRequest( "test-jar", testArtifacts, testJar, filters, relocators, resourceTransformers );

shader.shade( shadeTestRequest );
}

if ( createTestSourcesJar )
{
ShadeRequest shadeTestSourcesRequest =
createShadeSourcesRequest( testSourceArtifacts, testSourcesJar, filters, relocators,
resourceTransformers );
createShadeSourcesRequest( "test-sources-jar", testSourceArtifacts, testSourcesJar, filters,
relocators, resourceTransformers );

shader.shade( shadeTestSourcesRequest );
}
Expand Down Expand Up @@ -621,24 +624,24 @@ private void createErrorOutput()
getLog().error( "- You removed the configuration of the maven-jar-plugin that produces the main artifact." );
}

private ShadeRequest shadeRequest( Set<File> artifacts, File outputJar, List<Filter> filters,
private ShadeRequest shadeRequest( String shade, Set<File> artifacts, File outputJar, List<Filter> filters,
List<Relocator> relocators, List<ResourceTransformer> resourceTransformers )
{
ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars( artifacts );
shadeRequest.setUberJar( outputJar );
shadeRequest.setFilters( filters );
shadeRequest.setRelocators( relocators );
shadeRequest.setResourceTransformers( resourceTransformers );
shadeRequest.setResourceTransformers( toResourceTransformers( shade, resourceTransformers ) );
return shadeRequest;
}

private ShadeRequest createShadeSourcesRequest( Set<File> testArtifacts, File testJar, List<Filter> filters,
List<Relocator> relocators,
private ShadeRequest createShadeSourcesRequest( String shade, Set<File> testArtifacts, File testJar,
List<Filter> filters, List<Relocator> relocators,
List<ResourceTransformer> resourceTransformers )
{
ShadeRequest shadeSourcesRequest =
shadeRequest( testArtifacts, testJar, filters, relocators, resourceTransformers );
shadeRequest( shade, testArtifacts, testJar, filters, relocators, resourceTransformers );
shadeSourcesRequest.setShadeSourcesContent( shadeSourcesContent );
return shadeSourcesRequest;
}
Expand Down Expand Up @@ -1314,4 +1317,41 @@ public boolean updateExcludesInDeps( MavenProject project, List<Dependency> depe
session.getProjectBuildingRequest().setProject( original );
}
}

private List<ResourceTransformer> toResourceTransformers(
String shade, List<ResourceTransformer> resourceTransformers )
{
List<ResourceTransformer> forShade = new ArrayList<ResourceTransformer>();
ManifestResourceTransformer lastMt = null;
for ( ResourceTransformer transformer : resourceTransformers )
{
if ( !( transformer instanceof ManifestResourceTransformer ) )
{
forShade.add( transformer );
}
else if ( ( ( ManifestResourceTransformer ) transformer ) .isForShade( shade ) )
{
final ManifestResourceTransformer mt = (ManifestResourceTransformer) transformer;
if ( mt.isUsedForDefaultShading() && lastMt != null && !lastMt.isUsedForDefaultShading() )
{
continue; // skip, we already have a specific transformer
}
if ( !mt.isUsedForDefaultShading() && lastMt != null && lastMt.isUsedForDefaultShading() )
{
forShade.remove( lastMt );
}
else if ( !mt.isUsedForDefaultShading() && lastMt != null )
{
getLog().warn( "Ambiguous manifest transformer definition for '" + shade + "': "
+ mt + " / " + lastMt );
}
if ( lastMt == null || !mt.isUsedForDefaultShading() )
{
lastMt = mt;
}
forShade.add( transformer );
}
}
return forShade;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class ManifestResourceTransformer

private long time = Long.MIN_VALUE;

private String shade;

public void setMainClass( String mainClass )
{
this.mainClass = mainClass;
Expand Down Expand Up @@ -163,7 +165,14 @@ public void modifyOutputStream( JarOutputStream jos )
{
for ( Map.Entry<String, Object> entry : manifestEntries.entrySet() )
{
attributes.put( new Attributes.Name( entry.getKey() ), entry.getValue() );
if ( entry.getValue() == null )
{
attributes.remove( new Attributes.Name( entry.getKey() ) );
}
else
{
attributes.put( new Attributes.Name( entry.getKey() ), entry.getValue() );
}
}
}

Expand All @@ -188,4 +197,24 @@ private String relocate( String originalValue, List<Relocator> relocators )
}
return newValue;
}

/**
* The shades to apply this transformer to or no shades if no filter is applied.
*
* @param shade {@code null}, {@code jar}, {@code test-jar}, {@code sources-jar} or {@code test-sources-jar}.
*/
public void setForShade( String shade )
{
this.shade = shade;
}

public boolean isForShade( String shade )
{
return isUsedForDefaultShading() || this.shade.equalsIgnoreCase( shade );
}

public boolean isUsedForDefaultShading()
{
return this.shade == null || this.shade.isEmpty();
}
}
Loading