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

Switch to maven 4 and the new api #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
27 changes: 15 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<artifactId>maven-dependency-analyzer</artifactId>
<packaging>jar</packaging>
<version>1.13.1-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>

<name>Apache Maven Dependency Analyzer</name>
<description>
Expand Down Expand Up @@ -59,7 +59,8 @@
</distributionManagement>

<properties>
<mavenVersion>3.2.5</mavenVersion>
<mavenVersion>4.0.0-alpha-1-SNAPSHOT</mavenVersion>
<maven.plugin.tools.version>4.0.0-SNAPSHOT</maven.plugin.tools.version>
<javaVersion>8</javaVersion>
<project.build.outputTimestamp>2022-08-20T15:25:47Z</project.build.outputTimestamp>
</properties>
Expand All @@ -68,12 +69,7 @@

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<artifactId>maven-api-core</artifactId>
<version>${mavenVersion}</version>
</dependency>

Expand All @@ -86,7 +82,7 @@
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.4.2</version>
<version>${mavenVersion}</version>
</dependency>

<dependency>
Expand All @@ -104,9 +100,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -115,6 +111,12 @@
<version>3.23.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -149,6 +151,7 @@
<settingsFile>src/it/settings.xml</settingsFile>
<postBuildHookScript>verify</postBuildHookScript>
<goals>
<goal>-e</goal>
<goal>verify</goal>
</goals>
<properties>
Expand Down
26 changes: 7 additions & 19 deletions src/it/setup-mock-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,19 @@
<packaging>maven-plugin</packaging>
<version>1.0</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.release>8</maven.compiler.release>
</properties>

<dependencies>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>@mavenVersion@</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<artifactId>maven-api-core</artifactId>
<version>@mavenVersion@</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-tools-annotations</artifactId>
<version>@maven.plugin.tools.version@</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-dependency-analyzer</artifactId>
Expand Down
68 changes: 40 additions & 28 deletions src/it/setup-mock-plugin/src/main/java/it/test/MockAnalyzeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,34 @@
* under the License.
*/

import javax.inject.Inject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.api.Dependency;
import org.apache.maven.api.Project;
import org.apache.maven.api.ResolutionScope;
import org.apache.maven.api.Session;
import org.apache.maven.api.plugin.Log;
import org.apache.maven.api.plugin.MojoException;
import org.apache.maven.api.plugin.annotations.Component;
import org.apache.maven.api.plugin.annotations.LifecyclePhase;
import org.apache.maven.api.plugin.annotations.Mojo;
import org.apache.maven.api.plugin.annotations.Parameter;
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzer;

@Mojo( name = "mock-analyze", requiresDependencyResolution = ResolutionScope.TEST,
@Mojo( name = "mock-analyze",
requiresDependencyResolution = ResolutionScope.TEST,
defaultPhase = LifecyclePhase.VERIFY )
public class MockAnalyzeMojo extends AbstractMojo
public class MockAnalyzeMojo implements org.apache.maven.api.plugin.Mojo
{
class UnixPrintWiter extends PrintWriter
{
public UnixPrintWiter( File file ) throws FileNotFoundException
public UnixPrintWiter( Path file ) throws FileNotFoundException
{
super( file );
super( file.toFile() );
}

@Override
Expand All @@ -54,51 +55,62 @@ public void println()
}
}

@Inject
@Component
private ProjectDependencyAnalyzer analyzer;

@Inject
private MavenProject project;
@Parameter( defaultValue = "${session}", readonly = true, required = true )
private Session session;

@Parameter( defaultValue = "${project}", readonly = true, required = true )
private Project project;

@Parameter( defaultValue = "${project.build.directory}/analysis.txt", readonly = true )
private File output;
private Path output;

@Component
private Log log;

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

Files.createDirectories( output.toPath().getParent() );
Files.createDirectories( output.getParent() );
try ( PrintWriter printWriter = new UnixPrintWiter( output ) )
{
printWriter.println();

printWriter.println( "UsedDeclaredArtifacts:" );
analysis.getUsedDeclaredArtifacts().forEach( a -> printWriter.println( " " + a ) );
analysis.getUsedDeclaredArtifacts().forEach( a -> printWriter.println( " " + toString( a ) ) );
printWriter.println();

printWriter.println( "UsedUndeclaredArtifactsWithClasses:" );
analysis.getUsedUndeclaredArtifactsWithClasses().forEach( ( a, c ) -> {
printWriter.println( " " + a );
printWriter.println( " " + toString( a ) );
c.forEach( i -> printWriter.println( " " + i ) );
} );
printWriter.println();

printWriter.println( "UnusedDeclaredArtifacts:" );
analysis.getUnusedDeclaredArtifacts().forEach( a -> printWriter.println( " " + a ) );
analysis.getUnusedDeclaredArtifacts().forEach( a -> printWriter.println( " " + toString( a ) ) );
printWriter.println();

printWriter.println( "TestArtifactsWithNonTestScope:" );
analysis.getTestArtifactsWithNonTestScope().forEach( a -> printWriter.println( " " + a ) );
analysis.getTestArtifactsWithNonTestScope().forEach( a -> printWriter.println( " " + toString( a ) ) );
}
}
catch ( Exception e )
{
throw new MojoExecutionException( "analyze failed", e );
throw new MojoException( "analyze failed", e );
}

getLog().info( "Analyze done" );
log.info( "Analyze done" );
}

private String toString( Dependency a )
{
return a.key() + ":" + a.getScope().id();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Set;

/**
Expand All @@ -40,4 +41,14 @@ public interface ClassAnalyzer
*/
Set<String> analyze( URL url )
throws IOException;

/**
* <p>analyze.</p>
*
* @param path the JAR file or directory to analyze
* @return a {@link java.util.Set} object
* @throws java.io.IOException if any
*/
Set<String> analyze( Path path )
throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import java.io.IOException;
import java.io.InputStream;

/**
Expand All @@ -34,5 +35,13 @@ public interface ClassFileVisitor
* @param className a {@link java.lang.String} object.
* @param in a {@link java.io.InputStream} object.
*/
void visitClass( String className, InputStream in );
void visitClass( String className, InputStreamProvider in );

/**
* Provider for the input stream on the class file
*/
interface InputStreamProvider
{
InputStream open() throws IOException;
}
}
Loading