Skip to content

Commit bd6a925

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

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
/**
14+
* The default implementation simply log to debug and check for thread
15+
* interruption
16+
*/
17+
@Named("default")
18+
@Singleton
19+
@MojoExecutionScoped
20+
public class DefaultProgress implements Progress {
21+
22+
private final Logger logger = LoggerFactory.getLogger(DefaultProgress.class);
23+
private MojoExecution execution;
24+
private int work;
25+
26+
@Inject
27+
public DefaultProgress(MojoExecution execution) {
28+
this.execution = execution;
29+
}
30+
31+
@Override
32+
public void startTask(String task, int work) {
33+
setRemaining(work);
34+
logger.debug(execution.getExecutionId() + ": " + task);
35+
}
36+
37+
@Override
38+
public void worked(int work) {
39+
if (work == 0) {
40+
return;
41+
}
42+
if (work < 0) {
43+
logger.warn(execution.getExecutionId() + " reported negative amount of work!");
44+
}
45+
if (this.work < 0) {
46+
return;
47+
}
48+
if (work > this.work) {
49+
this.work = -1;
50+
logger.warn(execution.getExecutionId() + " reported more work than expected!");
51+
} else {
52+
this.work -= work;
53+
}
54+
55+
}
56+
57+
@Override
58+
public void setRemaining(int work) {
59+
this.work = work <= 0 ? -1 : work;
60+
}
61+
62+
@Override
63+
public boolean isCancelRequested() {
64+
return Thread.currentThread().isInterrupted();
65+
}
66+
67+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
48+
}

0 commit comments

Comments
 (0)