This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-analyzer.git
The following commit(s) were added to refs/heads/master by this push:
new d7df50a Skip module-info and package-info when listing the classes of
a dependency jar
d7df50a is described below
commit d7df50a632ae0a1a1a2af77bf72d4c9c77b38cad
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sat Nov 21 17:56:52 2026 +0100
Skip module-info and package-info when listing the classes of a dependency
jar
The jar fast path counted module-info.class as a class, so an aggregator
such as junit-jupiter never looked classless and was reported as unused. Use
the jar entry filter of the Path and Bytes sources.
---
.../jarWithClasslessAggregator/aggregator/pom.xml | 36 +++++++++++++++
.../main/java/aggregator/bundle/package-info.java | 24 ++++++++++
src/it/jarWithClasslessAggregator/api/pom.xml | 29 ++++++++++++
.../api/src/main/java/aggregator/api/Api.java | 21 +++++++++
src/it/jarWithClasslessAggregator/pom.xml | 34 ++++++++++++++
src/it/jarWithClasslessAggregator/usage/pom.xml | 53 ++++++++++++++++++++++
.../src/main/java/aggregator/usage/Usage.java | 25 ++++++++++
src/it/jarWithClasslessAggregator/verify.groovy | 33 ++++++++++++++
.../analyzer/DefaultProjectDependencyAnalyzer.java | 2 +-
.../DefaultProjectDependencyAnalyzerTest.java | 42 +++++++++++++++++
10 files changed, 298 insertions(+), 1 deletion(-)
diff --git a/src/it/jarWithClasslessAggregator/aggregator/pom.xml
b/src/it/jarWithClasslessAggregator/aggregator/pom.xml
new file mode 100644
index 0000000..5dd8cee
--- /dev/null
+++ b/src/it/jarWithClasslessAggregator/aggregator/pom.xml
@@ -0,0 +1,36 @@
+<?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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.maven.shared.dependency-analyzer.tests</groupId>
+ <artifactId>jarWithClasslessAggregatorBundle</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0</version>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven.shared.dependency-analyzer.tests</groupId>
+ <artifactId>jarWithClasslessAggregatorApi</artifactId>
+ <version>1.0</version>
+ </dependency>
+ </dependencies>
+</project>
diff --git
a/src/it/jarWithClasslessAggregator/aggregator/src/main/java/aggregator/bundle/package-info.java
b/src/it/jarWithClasslessAggregator/aggregator/src/main/java/aggregator/bundle/package-info.java
new file mode 100644
index 0000000..72690fd
--- /dev/null
+++
b/src/it/jarWithClasslessAggregator/aggregator/src/main/java/aggregator/bundle/package-info.java
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+/**
+ * The only class file of this jar, like module-info.class in junit-jupiter.
+ */
+@Deprecated
+package aggregator.bundle;
diff --git a/src/it/jarWithClasslessAggregator/api/pom.xml
b/src/it/jarWithClasslessAggregator/api/pom.xml
new file mode 100644
index 0000000..69785a1
--- /dev/null
+++ b/src/it/jarWithClasslessAggregator/api/pom.xml
@@ -0,0 +1,29 @@
+<?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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.maven.shared.dependency-analyzer.tests</groupId>
+ <artifactId>jarWithClasslessAggregatorApi</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0</version>
+
+</project>
diff --git
a/src/it/jarWithClasslessAggregator/api/src/main/java/aggregator/api/Api.java
b/src/it/jarWithClasslessAggregator/api/src/main/java/aggregator/api/Api.java
new file mode 100644
index 0000000..9af7bfa
--- /dev/null
+++
b/src/it/jarWithClasslessAggregator/api/src/main/java/aggregator/api/Api.java
@@ -0,0 +1,21 @@
+/*
+ * 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 aggregator.api;
+
+public class Api {}
diff --git a/src/it/jarWithClasslessAggregator/pom.xml
b/src/it/jarWithClasslessAggregator/pom.xml
new file mode 100644
index 0000000..52c4d88
--- /dev/null
+++ b/src/it/jarWithClasslessAggregator/pom.xml
@@ -0,0 +1,34 @@
+<?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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.maven.shared.dependency-analyzer.tests</groupId>
+ <artifactId>jarWithClasslessAggregator</artifactId>
+ <packaging>pom</packaging>
+ <version>1.0</version>
+
+ <modules>
+ <module>api</module>
+ <module>aggregator</module>
+ <module>usage</module>
+ </modules>
+</project>
diff --git a/src/it/jarWithClasslessAggregator/usage/pom.xml
b/src/it/jarWithClasslessAggregator/usage/pom.xml
new file mode 100644
index 0000000..db29df0
--- /dev/null
+++ b/src/it/jarWithClasslessAggregator/usage/pom.xml
@@ -0,0 +1,53 @@
+<?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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.maven.shared.dependency-analyzer.tests</groupId>
+ <artifactId>jarWithClasslessAggregatorUsage</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0</version>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven.shared.dependency-analyzer.tests</groupId>
+ <artifactId>jarWithClasslessAggregatorBundle</artifactId>
+ <version>1.0</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.shared.dependency-analyzer.tests</groupId>
+ <artifactId>maven-mock-plugin</artifactId>
+ <version>1.0</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>mock-analyze</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git
a/src/it/jarWithClasslessAggregator/usage/src/main/java/aggregator/usage/Usage.java
b/src/it/jarWithClasslessAggregator/usage/src/main/java/aggregator/usage/Usage.java
new file mode 100644
index 0000000..48fad37
--- /dev/null
+++
b/src/it/jarWithClasslessAggregator/usage/src/main/java/aggregator/usage/Usage.java
@@ -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 aggregator.usage;
+
+import aggregator.api.Api;
+
+public class Usage {
+ private final Api api = new Api();
+}
diff --git a/src/it/jarWithClasslessAggregator/verify.groovy
b/src/it/jarWithClasslessAggregator/verify.groovy
new file mode 100644
index 0000000..a2e05a4
--- /dev/null
+++ b/src/it/jarWithClasslessAggregator/verify.groovy
@@ -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.
+ */
+
+def analysis = new File( basedir, 'usage/target/analysis.txt' ).text
+
+def expected = '''
+UsedDeclaredArtifacts:
+
org.apache.maven.shared.dependency-analyzer.tests:jarWithClasslessAggregatorBundle:jar:1.0:compile
+
+UsedUndeclaredArtifactsWithClasses:
+
+UnusedDeclaredArtifacts:
+
+TestArtifactsWithNonTestScope:
+'''
+
+assert analysis == expected
diff --git
a/src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
b/src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
index c5e382a..86c9bc9 100644
---
a/src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
+++
b/src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
@@ -402,7 +402,7 @@ public class DefaultProjectDependencyAnalyzer implements
ProjectDependencyAnalyz
while (jarEntries.hasMoreElements()) {
String entry = jarEntries.nextElement().getName();
- if (entry.endsWith(".class")) {
+ if (ClassEntries.isClassEntry(entry)) {
String className = entry.replace('/', '.');
className = className.substring(0,
className.length() - ".class".length());
if (!excludedClasses.isMatch(className)) {
diff --git
a/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java
b/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java
index 76eedbb..3d1cc1b 100644
---
a/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java
+++
b/src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzerTest.java
@@ -18,6 +18,10 @@
*/
package org.apache.maven.shared.dependency.analyzer;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
@@ -26,7 +30,9 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.jar.JarOutputStream;
import java.util.stream.Collectors;
+import java.util.zip.ZipEntry;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
@@ -50,6 +56,7 @@ import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -91,6 +98,41 @@ class DefaultProjectDependencyAnalyzerTest {
repositorySystem, Collections.emptyList(), () -> mavenSession,
artifactHandlerManager);
}
+ @Test
+ void buildArtifactClassMapSkipsModuleInfoAndPackageInfoInJars(@TempDir
Path tempDir) throws IOException {
+ File aggregatorJar = jarWith(tempDir.resolve("aggregator.jar"),
"module-info.class", "META-INF/MANIFEST.MF");
+ File libraryJar = jarWith(
+ tempDir.resolve("library.jar"),
+ "module-info.class",
+ "org/example/package-info.class",
+ "org/example/Library.class",
+ "META-INF/versions/9/module-info.class");
+
+ Artifact aggregator = aTestArtifact("aggregator");
+ aggregator.setFile(aggregatorJar);
+ Artifact library = aTestArtifact("library");
+ library.setFile(libraryJar);
+
+ MavenProject project = new MavenProject();
+ project.setArtifacts(new LinkedHashSet<>(Arrays.asList(aggregator,
library)));
+
+ Map<Artifact, Set<String>> artifactClassMap =
+ analyzer.buildArtifactClassMap(project, new
ClassesPatterns(Collections.emptyList()));
+
+ assertEquals(Collections.emptySet(), artifactClassMap.get(aggregator));
+ assertEquals(Collections.singleton("org.example.Library"),
artifactClassMap.get(library));
+ }
+
+ private static File jarWith(Path path, String... entries) throws
IOException {
+ try (JarOutputStream jar = new
JarOutputStream(Files.newOutputStream(path))) {
+ for (String entry : entries) {
+ jar.putNextEntry(new ZipEntry(entry));
+ jar.closeEntry();
+ }
+ }
+ return path.toFile();
+ }
+
@Test
void testBuildClassToArtifactMap() {
Artifact artifact1 = aTestArtifact("artifact1");