Skip to content

Commit

Permalink
Switch to maven4 and the new api
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Dec 4, 2023
1 parent 4585c60 commit 05d4c48
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 183 deletions.
70 changes: 31 additions & 39 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ under the License.
</parent>

<artifactId>maven-clean-plugin</artifactId>
<version>3.3.3-SNAPSHOT</version>
<version>4.0.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Apache Maven Clean Plugin</name>
Expand Down Expand Up @@ -61,71 +61,48 @@ under the License.
</distributionManagement>

<properties>
<mavenVersion>3.5.4</mavenVersion>
<mavenVersion>4.0.0-alpha-9-SNAPSHOT</mavenVersion>
<project.build.outputTimestamp>2023-06-14T18:50:52Z</project.build.outputTimestamp>
<javaVersion>8</javaVersion>
</properties>

<dependencies>
<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</groupId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
<version>1.1.1</version>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>4.0.0</version>
</dependency>

<!-- dependencies to annotations -->
<!-- Test -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-xml</artifactId>
<version>4.0.2</version>
<scope>test</scope>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>4.0.0-alpha-2-SNAPSHOT</version>
<version>4.0.0-alpha-3-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-testing</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.plexus</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.0</version>
<classifier>no_aop</classifier>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -136,12 +113,27 @@ under the License.
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-resolver-provider</artifactId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.10.3-SNAPSHOT</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>run-its</id>
Expand Down
4 changes: 4 additions & 0 deletions src/it/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ under the License.
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</snapshots>
</repository>
</repositories>
Expand All @@ -44,9 +46,11 @@ under the License.
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
Expand Down
68 changes: 37 additions & 31 deletions src/main/java/org/apache/maven/plugins/clean/CleanMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
*/
package org.apache.maven.plugins.clean;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import jakarta.inject.Inject;
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.Mojo;
import org.apache.maven.api.plugin.annotations.Parameter;

/**
* Goal which cleans the build.
Expand All @@ -43,40 +45,43 @@
* @see org.apache.maven.plugins.clean.Fileset
* @since 2.0
*/
@Mojo(name = "clean", threadSafe = true)
public class CleanMojo extends AbstractMojo {
@Mojo(name = "clean")
public class CleanMojo implements org.apache.maven.api.plugin.Mojo {

public static final String FAST_MODE_BACKGROUND = "background";

public static final String FAST_MODE_AT_END = "at-end";

public static final String FAST_MODE_DEFER = "defer";

@Inject
private Log logger;

/**
* This is where build results go.
*/
@Parameter(defaultValue = "${project.build.directory}", readonly = true, required = true)
private File directory;
private Path directory;

/**
* This is where compiled classes go.
*/
@Parameter(defaultValue = "${project.build.outputDirectory}", readonly = true, required = true)
private File outputDirectory;
private Path outputDirectory;

/**
* This is where compiled test classes go.
*/
@Parameter(defaultValue = "${project.build.testOutputDirectory}", readonly = true, required = true)
private File testOutputDirectory;
private Path testOutputDirectory;

/**
* This is where the site plugin generates its pages.
*
* @since 2.1.1
*/
@Parameter(defaultValue = "${project.build.outputDirectory}", readonly = true, required = true)
private File reportDirectory;
private Path reportDirectory;

/**
* Sets whether the plugin runs in verbose mode. As of plugin version 2.3, the default value is derived from Maven's
Expand Down Expand Up @@ -190,7 +195,7 @@ public class CleanMojo extends AbstractMojo {
* @see #fast
*/
@Parameter(property = "maven.clean.fastDir")
private File fastDir;
private Path fastDir;

/**
* Mode to use when using fast clean. Values are: <code>background</code> to start deletion immediately and
Expand All @@ -206,32 +211,32 @@ public class CleanMojo extends AbstractMojo {
private String fastMode;

@Parameter(defaultValue = "${session}", readonly = true)
private MavenSession session;
private Session session;

/**
* Deletes file-sets in the following project build directory order: (source) directory, output directory, test
* directory, report directory, and then the additional file-sets.
*
* @throws MojoExecutionException When a directory failed to get deleted.
* @see org.apache.maven.plugin.Mojo#execute()
* @throws MojoException When a directory failed to get deleted.
* @see org.apache.maven.api.plugin.Mojo#execute()
*/
public void execute() throws MojoExecutionException {
public void execute() {
if (skip) {
getLog().info("Clean is skipped.");
logger.info("Clean is skipped.");
return;
}

String multiModuleProjectDirectory =
session != null ? session.getSystemProperties().getProperty("maven.multiModuleProjectDirectory") : null;
File fastDir;
session != null ? session.getSystemProperties().get("maven.multiModuleProjectDirectory") : null;
Path fastDir;
if (fast && this.fastDir != null) {
fastDir = this.fastDir;
} else if (fast && multiModuleProjectDirectory != null) {
fastDir = new File(multiModuleProjectDirectory, "target/.clean");
fastDir = Paths.get(multiModuleProjectDirectory, "target/.clean");
} else {
fastDir = null;
if (fast) {
getLog().warn("Fast clean requires maven 3.3.1 or newer, "
logger.warn("Fast clean requires maven 3.3.1 or newer, "
+ "or an explicit directory to be specified with the 'fastDir' configuration of "
+ "this plugin, or the 'maven.clean.fastDir' user property to be set.");
}
Expand All @@ -244,10 +249,10 @@ public void execute() throws MojoExecutionException {
+ FAST_MODE_BACKGROUND + "', '" + FAST_MODE_AT_END + "' and '" + FAST_MODE_DEFER + "'.");
}

Cleaner cleaner = new Cleaner(session, getLog(), isVerbose(), fastDir, fastMode);
Cleaner cleaner = new Cleaner(session, logger, isVerbose(), fastDir, fastMode);

try {
for (File directoryItem : getDirectories()) {
for (Path directoryItem : getDirectories()) {
if (directoryItem != null) {
cleaner.delete(directoryItem, null, followSymLinks, failOnError, retryOnError);
}
Expand All @@ -256,7 +261,7 @@ public void execute() throws MojoExecutionException {
if (filesets != null) {
for (Fileset fileset : filesets) {
if (fileset.getDirectory() == null) {
throw new MojoExecutionException("Missing base directory for " + fileset);
throw new MojoException("Missing base directory for " + fileset);
}
final String[] includes = fileset.getIncludes();
final String[] excludes = fileset.getExcludes();
Expand All @@ -273,8 +278,9 @@ public void execute() throws MojoExecutionException {
fileset.getDirectory(), selector, fileset.isFollowSymlinks(), failOnError, retryOnError);
}
}

} catch (IOException e) {
throw new MojoExecutionException("Failed to clean project: " + e.getMessage(), e);
throw new MojoException("Failed to clean project: " + e.getMessage(), e);
}
}

Expand All @@ -284,20 +290,20 @@ public void execute() throws MojoExecutionException {
* @return <code>true</code> if verbose output is enabled, <code>false</code> otherwise.
*/
private boolean isVerbose() {
return (verbose != null) ? verbose : getLog().isDebugEnabled();
return (verbose != null) ? verbose : logger.isDebugEnabled();
}

/**
* Gets the directories to clean (if any). The returned array may contain null entries.
*
* @return The directories to clean or an empty array if none, never <code>null</code>.
*/
private File[] getDirectories() {
File[] directories;
private Path[] getDirectories() {
Path[] directories;
if (excludeDefaultDirectories) {
directories = new File[0];
directories = new Path[0];
} else {
directories = new File[] {directory, outputDirectory, testOutputDirectory, reportDirectory};
directories = new Path[] {directory, outputDirectory, testOutputDirectory, reportDirectory};
}
return directories;
}
Expand Down
Loading

0 comments on commit 05d4c48

Please sign in to comment.