Skip to content
Open
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
52 changes: 52 additions & 0 deletions src/it/projects/apache-notice-project-name/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.shade</groupId>
<artifactId>apache-notice-project-name</artifactId>
<version>1.0</version>
<name>Maven Shade Notice Reproducer</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dependency notice
28 changes: 28 additions & 0 deletions src/it/projects/apache-notice-project-name/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.JarFile

def shadedJar = new JarFile(new File(basedir, "target/apache-notice-project-name-1.0.jar"))
try {
def notice = shadedJar.getInputStream(shadedJar.getJarEntry("META-INF/NOTICE")).getText("UTF-8")
assert notice.contains("Version 2.0, in this case for Maven Shade Notice Reproducer")
} finally {
shadedJar.close()
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.apache.maven.plugins.shade.relocation.Relocator;
import org.apache.maven.plugins.shade.relocation.SerializedLambdaRelocator;
import org.apache.maven.plugins.shade.relocation.SimpleRelocator;
import org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer;
import org.apache.maven.plugins.shade.resource.ManifestResourceTransformer;
import org.apache.maven.plugins.shade.resource.ResourceTransformer;
import org.apache.maven.project.DefaultProjectBuildingRequest;
Expand Down Expand Up @@ -971,6 +972,9 @@ private List<ResourceTransformer> getResourceTransformers() throws MojoExecution
throw new MojoExecutionException(
"Failed to create shaded artifact: parameter transformers contains null (double-check XML attribute)");
}
if (transformer instanceof ApacheNoticeResourceTransformer) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

might be necessary, but it would be cleaner if we could figure out some way to do this that doesn't require instanceof and casting.

((ApacheNoticeResourceTransformer) transformer).setProjectNameIfUnset(project.getName());
}
}
return Arrays.asList(transformers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ public class ApacheNoticeResourceTransformer extends AbstractCompatibilityTransf

private static final String NOTICE_MD_PATH = "META-INF/NOTICE.md";

/**
* Uses the Maven project name when no project name was configured explicitly.
*
* @param projectName the Maven project name
*/
public void setProjectNameIfUnset(String projectName) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why "if unset"? why not just set project name?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because the field can be set via XML in the POM file, too. That takes precedence. If it is however not set via XML in POM, it will be set here. Thus if unset.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This still makes me queasy. My gut is that if someone calls this method, the project name should be set.

Even if we keep the behavior as it, we need a better name. setDefaultProjectName? setFallbackProjectName? maybe something else.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The current name is descriptive. It does not need a different name.

if (this.projectName.isEmpty()
&& projectName != null
&& !projectName.trim().isEmpty()) {
this.projectName = projectName;
}
}

@Override
public boolean canTransformResource(String resource) {
return NOTICE_PATH.equalsIgnoreCase(resource)
Expand Down
3 changes: 3 additions & 0 deletions src/site/markdown/examples/resource-transformers.md.vm
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ For example, the following prevents the license from a `commons-collections` dep

Some licenses (including the [ Apache License, Version 2](https://www.apache.org/licenses/LICENSE-2.0.html)) require that notices are preserved by downstream distributors. `ApacheNoticeResourceTransformer` automates the assembly of an appropriate `NOTICE`.

The generated notice uses the Maven project name by default. This can be overridden with the
`projectName` transformer parameter.

For example, to simply merge in dependent notices:

```xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -65,6 +66,25 @@ public void testCanTransformResource() {
assertFalse(transformer.canTransformResource("META-INF/MANIFEST.MF"));
}

@Test
public void testProjectNameDefaultsToMavenProjectName() {
transformer.setProjectNameIfUnset(null);
transformer.setProjectNameIfUnset(" ");
assertEquals("", transformer.projectName);

transformer.setProjectNameIfUnset("Maven Project Name");
assertEquals("Maven Project Name", transformer.projectName);
}

@Test
public void testConfiguredProjectNameTakesPrecedence() {
transformer.projectName = "Configured Project Name";

transformer.setProjectNameIfUnset("Maven Project Name");

assertEquals("Configured Project Name", transformer.projectName);
}

@Test
public void testNoParametersShouldNotThrowNullPointerWhenNoInput() throws IOException {
processAndFailOnNullPointer("");
Expand Down
Loading