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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ The project was relocated from <https://github.com/sonatype/sisu-build-api>. Als

## Provided APIs

### Progress

The API allows a mojo to report progress in a way that is suitable to be shown as a progressbar as well as check if the user wants the mojo to gracefully abort its current operation.
This can be useful for example when processing some files in a loop so the user can directly see the amount of progress and possibly ask to abort if it takes to long.

### IDE connection to maven process

This API is usually not used by mojos but for IDE integration, if enabled as a maven-core extension plexus-build-api supply a way to communicate with the running maven build and get events.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.codehaus.plexus.build.progress;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.maven.execution.scope.MojoExecutionScoped;
import org.apache.maven.plugin.MojoExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The default implementation simply log to debug and check for thread
* interruption
*/
@Named("default")
@Singleton
@MojoExecutionScoped
public class DefaultProgress implements Progress {

private final Logger logger = LoggerFactory.getLogger(DefaultProgress.class);
private MojoExecution execution;
private int work;

@Inject
public DefaultProgress(MojoExecution execution) {
this.execution = execution;
}

@Override
public void startTask(String task, int work) {
setRemaining(work);
logger.debug(execution.getExecutionId() + ": " + task);
}

@Override
public void worked(int work) {
if (work == 0) {
return;
}
if (work < 0) {
logger.warn(execution.getExecutionId() + " reported negative amount of work!");
}
if (this.work < 0) {
return;
}
if (work > this.work) {
this.work = -1;
logger.warn(execution.getExecutionId() + " reported more work than expected!");
} else {
this.work -= work;
}
}

@Override
public void setRemaining(int work) {
this.work = work <= 0 ? -1 : work;
}

@Override
public boolean isCancelRequested() {
return Thread.currentThread().isInterrupted();
}
}
48 changes: 48 additions & 0 deletions src/main/java/org/codehaus/plexus/build/progress/Progress.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.codehaus.plexus.build.progress;

/**
* The {@link Progress} allows a mojo to report its current state and check if
* the user has requested cancellation.
*/
public interface Progress {

/**
* Reports that the mojo has started the given task with the given amount of
* work.
*
* @param task
* @param work the amount of work that the mojo plan to report, if a value
* <code>&lt;= 0</code> is given the amount of work is assumed to be
* unknown.
*/
void startTask(String task, int work);

/**
* Reports a given amount of work was processed
*
* @param work
*/
void worked(int work);

/**
* Reports one unit of work to be processed
*/
default void worked() {
worked(1);
}

/**
* Notifies the remaining amount of work that will be reported
*
* @param work
*/
void setRemaining(int work);

/**
* This method should be used to check if the user has requested to finish the
* current work and break out early.
*
* @return <code>true</code> if a cancel request is currently pending.
*/
boolean isCancelRequested();
}
Loading