Skip to content

Commit 2e729e0

Browse files
Christoph Läubrichlaeubi
authored andcommitted
Add a Progress API
Fix #52 Fix #56
1 parent e5c2a61 commit 2e729e0

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-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: 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 task
14+
* @param work the amount of work that the mojo plan to report, if a value
15+
* <code>&lt;= 0</code> is given the amount of work is assumed to be
16+
* unknown.
17+
*/
18+
void startTask(String task, int work);
19+
20+
/**
21+
* Reports a given amount of work was processed
22+
*
23+
* @param work
24+
*/
25+
void worked(int work);
26+
27+
/**
28+
* Reports one unit of work to be processed
29+
*/
30+
default void worked() {
31+
worked(1);
32+
}
33+
34+
/**
35+
* Notifies the remaining amount of work that will be reported
36+
*
37+
* @param work
38+
*/
39+
void setRemaining(int work);
40+
41+
/**
42+
* This method should be used to check if the user has requested to finish the
43+
* current work and break out early.
44+
*
45+
* @return <code>true</code> if a cancel request is currently pending.
46+
*/
47+
boolean isCancelRequested();
48+
}

0 commit comments

Comments
 (0)