Skip to content

Commit

Permalink
Can configure notification message type (short/full).
Browse files Browse the repository at this point in the history
A short message will only display the time spent building the project
in case of success.
The full message is the one describing each module build time and status.

Property: notification.message.short = false (by default).
  • Loading branch information
jcgay committed Mar 15, 2014
1 parent 6df1c9e commit f7467c5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ protected Status getBuildStatus(MavenExecutionResult result) {
}

protected String buildNotificationMessage(MavenExecutionResult result) {
if (configuration.isShortDescription()) {
return buildShortDescription(result);
}
return buildFullDescription(result);
}

protected String buildShortDescription(MavenExecutionResult result) {
if (getBuildStatus(result) == Status.SUCCESS) {
StringBuilder builder = new StringBuilder("Built in: ");
builder.append(stopwatch.elapsedTime(TimeUnit.SECONDS));
builder.append(" second(s).");
return builder.toString();
}

return "";
}

private String buildFullDescription(MavenExecutionResult result) {
StringBuilder builder = new StringBuilder();
for (MavenProject project : result.getTopologicallySortedProjects()) {
BuildSummary buildSummary = result.getBuildSummary(project);
Expand All @@ -76,11 +94,13 @@ protected String buildNotificationMessage(MavenExecutionResult result) {
}

protected String buildTitle(MavenExecutionResult result) {
StringBuilder builder = new StringBuilder();
return builder.append(result.getProject().getName())
.append(" [")
.append(stopwatch.elapsedTime(TimeUnit.SECONDS))
.append("s]")
.toString();
StringBuilder builder = new StringBuilder().append(result.getProject().getName());
if (!configuration.isShortDescription()) {
builder.append(" [")
.append(stopwatch.elapsedTime(TimeUnit.SECONDS))
.append("s]");

}
return builder.toString();
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/github/jcgay/maven/notifier/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Configuration {
private String snarlPort;
private String snarlHost;
private String notificationCenterSound;
private boolean shortDescription;

public void setImplementation(String implementation) {
this.implementation = implementation;
Expand Down Expand Up @@ -113,6 +114,14 @@ public String getNotificationCenterSound() {
return notificationCenterSound;
}

public boolean isShortDescription() {
return shortDescription;
}

public void setShortDescription(boolean shortDescription) {
this.shortDescription = shortDescription;
}

@Override
public String toString() {
return Objects.toStringHelper(this)
Expand All @@ -127,6 +136,7 @@ public String toString() {
.add("systemTrayWaitBeforeEnd", systemTrayWaitBeforeEnd)
.add("snarlPort", snarlPort)
.add("snarlHost", snarlHost)
.add("shortDescription", shortDescription)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import static com.github.jcgay.maven.notifier.ConfigurationParser.ConfigurationProperties.Property.NOTIFICATION_CENTER_SOUND;
import static com.github.jcgay.maven.notifier.ConfigurationParser.ConfigurationProperties.Property.NOTIFY_SEND_PATH;
import static com.github.jcgay.maven.notifier.ConfigurationParser.ConfigurationProperties.Property.NOTIFY_SEND_TIMEOUT;
import static com.github.jcgay.maven.notifier.ConfigurationParser.ConfigurationProperties.Property.SHORT_DESCRIPTION;
import static com.github.jcgay.maven.notifier.ConfigurationParser.ConfigurationProperties.Property.SNARL_HOST;
import static com.github.jcgay.maven.notifier.ConfigurationParser.ConfigurationProperties.Property.SNARL_PORT;
import static com.github.jcgay.maven.notifier.ConfigurationParser.ConfigurationProperties.Property.SYSTEM_TRAY_WAIT;
import static java.lang.Boolean.parseBoolean;

@Component(role = ConfigurationParser.class, hint = "maven-notifier-configuration")
public class ConfigurationParser {
Expand Down Expand Up @@ -78,6 +80,7 @@ private Configuration parse(ConfigurationProperties properties) {
configuration.setSystemTrayWaitBeforeEnd(properties.get(SYSTEM_TRAY_WAIT));
configuration.setSnarlPort(properties.get(SNARL_PORT));
configuration.setSnarlHost(properties.get(SNARL_HOST));
configuration.setShortDescription(parseBoolean(properties.get(SHORT_DESCRIPTION)));
return configuration;
}

Expand Down Expand Up @@ -139,7 +142,8 @@ public static enum Property {
SYSTEM_TRAY_WAIT("notifier.system-tray.wait", String.valueOf(TimeUnit.SECONDS.toMillis(2))),
SNARL_PORT("notifier.snarl.port", String.valueOf(9887)),
SNARL_HOST("notifier.snarl.host", "localhost"),
NOTIFICATION_CENTER_SOUND("notifier.notification-center.sound");
NOTIFICATION_CENTER_SOUND("notifier.notification-center.sound"),
SHORT_DESCRIPTION("notifier.message.short", "false");

private String key;
private String defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import com.github.jcgay.maven.notifier.AbstractCustomEventSpy;
import com.github.jcgay.maven.notifier.Configuration;
import com.github.jcgay.maven.notifier.Notifier;
import com.github.jcgay.maven.notifier.Status;
import com.github.jcgay.maven.notifier.executor.Executor;
import com.github.jcgay.maven.notifier.executor.RuntimeExecutor;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import org.apache.maven.execution.MavenExecutionResult;
import org.codehaus.plexus.component.annotations.Component;

import java.util.concurrent.TimeUnit;

@Component(role = Notifier.class, hint = "notification-center")
public class NotificationCenterNotifier extends AbstractCustomEventSpy {

Expand Down Expand Up @@ -44,14 +41,7 @@ public void onEvent(MavenExecutionResult event) {

@Override
protected String buildNotificationMessage(MavenExecutionResult result) {
if (getBuildStatus(result) == Status.SUCCESS) {
StringBuilder builder = new StringBuilder("Built in: ");
builder.append(stopwatch.elapsedTime(TimeUnit.SECONDS));
builder.append(" second(s).");
return builder.toString();
}

return "";
return buildShortDescription(result);
}

private String[] buildCommand(MavenExecutionResult result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import static com.github.jcgay.maven.notifier.ConfigurationParser.ConfigurationProperties.Property;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class ConfigurationParserTest {

Expand Down Expand Up @@ -69,6 +71,7 @@ public void should_return_default_configuration() throws Exception {
assertEquals(Long.valueOf(result.getSystemTrayWaitBeforeEnd()), Long.valueOf(Property.SYSTEM_TRAY_WAIT.defaultValue()));
assertEquals(Integer.valueOf(result.getSnarlPort()), Integer.valueOf(Property.SNARL_PORT.defaultValue()));
assertEquals(result.getSnarlHost(), Property.SNARL_HOST.defaultValue());
assertFalse(result.isShortDescription());
}

@Test
Expand All @@ -87,6 +90,7 @@ public void should_return_configuration() {
properties.put(Property.SYSTEM_TRAY_WAIT.key(), "1");
properties.put(Property.SNARL_PORT.key(), "1");
properties.put(Property.SNARL_HOST.key(), "192.168.1.11");
properties.put(Property.SHORT_DESCRIPTION.key(), "true");

Configuration result = parser.get(properties);

Expand All @@ -102,5 +106,6 @@ public void should_return_configuration() {
assertEquals(result.getSystemTrayWaitBeforeEnd(), 1);
assertEquals(result.getSnarlPort(), 1);
assertEquals(result.getSnarlHost(), "192.168.1.11");
assertTrue(result.isShortDescription());
}
}

0 comments on commit f7467c5

Please sign in to comment.