Skip to content

Commit

Permalink
[MSHARED-1393] Allow to exclude classes from verification
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 9, 2024
1 parent 7231d5a commit c82f67a
Show file tree
Hide file tree
Showing 22 changed files with 565 additions and 77 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down Expand Up @@ -169,6 +174,7 @@
<localRepositoryPath>target/local-repo</localRepositoryPath>
<settingsFile>src/it/settings.xml</settingsFile>
<postBuildHookScript>verify</postBuildHookScript>
<preBuildHookScript>setup</preBuildHookScript>
<goals>
<goal>verify</goal>
</goals>
Expand Down
64 changes: 64 additions & 0 deletions src/it/excludeClassFromJar/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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>jarWithXercesDependencies</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</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>
<configuration>
<excludedClasses>
<exclude>org.xml.sax.*</exclude>
</excludedClasses>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jarWithXmlTransitiveDependency;

/*
* 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 org.dom4j.Text;
import org.xml.sax.Parser;

/**
* Dependency on dom4j gives xml-apis transitive dependency, which contains SAX Parser class. But SAX Parser is available in
* JDK: no need to declare a direct dependency.
*
*/
public class Project
{
public Text text;

public Parser parser;

// constructors -----------------------------------------------------------

public Project()
{
// no op
}
}
33 changes: 33 additions & 0 deletions src/it/excludeClassFromJar/verify.groovy
Original file line number Diff line number Diff line change
@@ -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, 'target/analysis.txt' ).text

def expected = '''
UsedDeclaredArtifacts:
dom4j:dom4j:jar:1.6.1:compile
UsedUndeclaredArtifactsWithClasses:
UnusedDeclaredArtifacts:
TestArtifactsWithNonTestScope:
'''

assert analysis == expected
56 changes: 56 additions & 0 deletions src/it/excludeClassFromProject/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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>jarWithXercesDependencies</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<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>
<configuration>
<excludedClasses>
<exclude>org.example.BadClass</exclude>
</excludedClasses>
</configuration>
</plugin>
</plugins>
</build>

</project>
24 changes: 24 additions & 0 deletions src/it/excludeClassFromProject/setup.groovy
Original file line number Diff line number Diff line change
@@ -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.
*/
def badClass = new File(basedir, 'target/classes/org/example/BadClass.class')

badClass.getParentFile().mkdirs()
badClass << 'some content'

assert badClass.isFile()
32 changes: 32 additions & 0 deletions src/it/excludeClassFromProject/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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, 'target/analysis.txt' ).text

def expected = '''
UsedDeclaredArtifacts:
UsedUndeclaredArtifactsWithClasses:
UnusedDeclaredArtifacts:
TestArtifactsWithNonTestScope:
'''

assert analysis == expected
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.Set;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -63,12 +64,15 @@ public void println()
@Parameter( defaultValue = "${project.build.directory}/analysis.txt", readonly = true )
private File output;

@Parameter
private Set<String> excludedClasses;

@Override
public void execute() throws MojoExecutionException, MojoFailureException
{
try
{
ProjectDependencyAnalysis analysis = analyzer.analyze( project );
ProjectDependencyAnalysis analysis = analyzer.analyze( project, excludedClasses );

Files.createDirectories( output.toPath().getParent() );
try ( PrintWriter printWriter = new UnixPrintWiter( output ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,16 @@ public interface ClassAnalyzer {
* @return a {@link java.util.Set} object
* @throws java.io.IOException if any
*/
Set<String> analyze(URL url) throws IOException;
default Set<String> analyze(URL url) throws IOException {
return analyze(url, new ClassesPatterns());
}

/**
* <p>analyze.</p>
*
* @param url the JAR file or directory to analyze
* @return a {@link java.util.Set} object
* @throws java.io.IOException if any
*/
Set<String> analyze(URL url, ClassesPatterns excludedClasses) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.shared.dependency.analyzer;

import java.util.Collection;
import java.util.Collections;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Patterns for classes
*/
public class ClassesPatterns {

private final Collection<Pattern> patterns;

/**
* Default constructor.
*
* @param patterns a patterns to mach
*/
public ClassesPatterns(Collection<String> patterns) {
if (patterns == null) {
this.patterns = Collections.emptyList();
} else {
this.patterns = patterns.stream().map(Pattern::compile).collect(Collectors.toSet());
}
}

public ClassesPatterns() {
this.patterns = Collections.emptySet();
}

public boolean isMatch(String className) {
if (patterns.isEmpty()) {
return false;
}
return patterns.stream().anyMatch(pattern -> pattern.matcher(className).matches());
}
}
Loading

0 comments on commit c82f67a

Please sign in to comment.