Skip to content

Commit ddf3a9f

Browse files
author
Christoph Läubrich
committed
Add a Progress API
Fix #52 Fix #56
1 parent c99cf7e commit ddf3a9f

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ The project was relocated from <https://github.com/sonatype/sisu-build-api>. Als
3232

3333
## Provided APIs
3434

35+
### Progress
36+
37+
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.
38+
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.
39+
3540
### IDE connection to maven process
3641

3742
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.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.codehaus.plexus.build.progress;
2+
3+
import javax.inject.Inject;
4+
import javax.inject.Named;
5+
import javax.inject.Singleton;
6+
7+
import org.apache.maven.execution.scope.MojoExecutionScoped;
8+
import org.apache.maven.plugin.MojoExecution;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
/**
13+
* The default implementation simply log to debug and check for thread
14+
* interruption
15+
*/
16+
@Named("default")
17+
@Singleton
18+
@MojoExecutionScoped
19+
public class DefaultProgress implements Progress {
20+
21+
private final Logger logger = LoggerFactory.getLogger(DefaultProgress.class);
22+
private MojoExecution execution;
23+
private int work;
24+
25+
@Inject
26+
public DefaultProgress(MojoExecution execution) {
27+
this.execution = execution;
28+
}
29+
30+
@Override
31+
public void startTask(String task, int work) {
32+
setRemaining(work);
33+
logger.debug(execution.getExecutionId() + ": " + task);
34+
}
35+
36+
@Override
37+
public void worked(int work) {
38+
if (work == 0) {
39+
return;
40+
}
41+
if (work < 0) {
42+
logger.warn(execution.getExecutionId() + " reported negative amount of work!");
43+
}
44+
if (this.work < 0) {
45+
return;
46+
}
47+
if (work > this.work) {
48+
this.work = -1;
49+
logger.warn(execution.getExecutionId() + " reported more work than expected!");
50+
} else {
51+
this.work -= work;
52+
}
53+
}
54+
55+
@Override
56+
public void setRemaining(int work) {
57+
this.work = work <= 0 ? -1 : work;
58+
}
59+
60+
@Override
61+
public boolean isCancelRequested() {
62+
return Thread.currentThread().isInterrupted();
63+
}
64+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.codehaus.plexus.build.progress;
2+
3+
/**
4+
* The {@link Progress} allows a mojo to report its current state and check if
5+
* the user has requested cancellation.
6+
*/
7+
public interface Progress {
8+
9+
/**
10+
* Reports that the mojo has started the given task with the given amount of
11+
* work.
12+
*
13+
* @param name
14+
* @param work the amount of work that the mojo plan to report, if a value <= 0
15+
* is given the amount of work is assumed to be unknown.
16+
*/
17+
void startTask(String task, int work);
18+
19+
/**
20+
* Reports a given amount of work was processed
21+
*
22+
* @param work
23+
*/
24+
void worked(int work);
25+
26+
/**
27+
* Reports one unit of work to be processed
28+
*/
29+
default void worked() {
30+
worked(1);
31+
}
32+
33+
/**
34+
* Notifies the remaining amount of work that will be reported
35+
*
36+
* @param work
37+
*/
38+
void setRemaining(int work);
39+
40+
/**
41+
* This method should be used to check if the user has requested to finish the
42+
* current work and break out early.
43+
*
44+
* @return <code>true</code> if a cancel request is currently pending.
45+
*/
46+
boolean isCancelRequested();
47+
}

0 commit comments

Comments
 (0)