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
45 changes: 45 additions & 0 deletions src/it/automatic-name/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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.plugins</groupId>
<artifactId>automatic-name</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>automatic-name-it</name>
<organization>
<name>jar plugin it</name>
</organization>
<properties>
<maven.compiler.release>11</maven.compiler.release>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<automaticModuleName>org.apache.maven.its.it.issue520</automaticModuleName>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 org.apache.maven.its.it.issue520;

public final class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
74 changes: 74 additions & 0 deletions src/it/automatic-name/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

/*
* 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.*;
import java.util.jar.*;
import org.apache.commons.io.*;

boolean result = true;

try
{
File target = new File( basedir, "target" );
if ( !target.exists() || !target.isDirectory() )
{
System.err.println( "target file is missing or not a directory." );
return false;
}

File artifact = new File( target, "automatic-name-1.0-SNAPSHOT.jar" );
if ( !artifact.exists() || artifact.isDirectory() )
{
System.err.println( "artifact file " + file + " is missing or a directory." );
return false;
}

JarFile jar = new JarFile( artifact );
Enumeration jarEntries = jar.entries();
while ( jarEntries.hasMoreElements() )
{
JarEntry entry = (JarEntry) jarEntries.nextElement();
if ( !entry.isDirectory() )
{
// Only compare files
if ( entry.getName().equals( "META-INF/MANIFEST.MF" ) )
{
String manifest = IOUtils.toString( jar.getInputStream ( entry ) );
int index = manifest.indexOf( "Automatic-Module-Name: org.apache.maven.its.it.issue520" );
if ( index <= 0 )
{
System.err.println( "MANIFEST doesn't contain: 'Automatic-Module-Name: org.apache.maven.its.it.issue520'" );
return false;
}
return true;
}
}
}
System.err.println( "MANIFEST.MF not found" );
return false;
}
catch( Throwable e )
{
e.printStackTrace();
result = false;
}

return result;
18 changes: 18 additions & 0 deletions src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ public abstract class AbstractJarMojo extends AbstractMojo {
@Parameter(property = "maven.jar.detectMultiReleaseJar", defaultValue = "true")
private boolean detectMultiReleaseJar;

/**
* Establish the automatic module name of the module in the JAR.
* Ignored if the JAR contains a {@code module-info.class} file.
*
* @since 3.6.0
*/
@Parameter(property = "maven.jar.automaticModuleName")
private String automaticModuleName;

/**
* If set to {@code false}, the files and directories that by default are excluded from the resulting archive,
* like {@code .gitignore}, {@code .cvsignore} etc. will be included.
Expand Down Expand Up @@ -297,6 +306,15 @@ public File createArchive() throws MojoExecutionException {
boolean containsModuleDescriptor =
Arrays.stream(includedFiles).anyMatch(p -> p.endsWith(MODULE_DESCRIPTOR_FILE_NAME));

if (automaticModuleName != null && !automaticModuleName.isEmpty()) {
if (containsModuleDescriptor) {
getLog().warn("Not setting automatic module name, because a module descriptor was found.");
} else {
getLog().debug("Adding 'Automatic-Module-Name: " + automaticModuleName + "' manifest entry.");
archive.addManifestEntry("Automatic-Module-Name", automaticModuleName);
}
}

String archiverName = containsModuleDescriptor ? "mjar" : "jar";

MavenArchiver archiver = new MavenArchiver();
Expand Down