Skip to content

Commit

Permalink
Make the jar executable to allow for easier testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
rombert committed Oct 16, 2013
1 parent cf1c0e5 commit f88277a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 69 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>ro.lmn.maven.mdn.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/ro/lmn/maven/mdn/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2008-2013 Robert Munteanu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.lmn.maven.mdn;

import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException, InterruptedException {

Notifier notifier = new Notifier();

if (args.length > 0 && "failure".equals(args[0])) {
notifier.notifySend("Build failed", "Something went wrong...", Notifier.ICON_ERROR);
} else {
notifier.notifySend("Build successful", "All is right", Notifier.ICON_INFO);
}

}

}
85 changes: 85 additions & 0 deletions src/main/java/ro/lmn/maven/mdn/Notifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2008-2013 Robert Munteanu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.lmn.maven.mdn;

import java.io.IOException;

public class Notifier {

public static final String ICON_INFO = "dialog-information";
public static final String ICON_ERROR = "dialog-error";

public void notifySend(String title, String details, String icon) throws IOException {

ProcessBuilder builder = null;
OSName os = OSName.getConstantForValue(System.getProperty("os.name"));
if (os != null) {
switch (os) {
case LINUX:
builder = prepareLinuxNotifier(title, details, icon, 2);
break;
case MAC:
builder = new ProcessBuilder("terminal-notifier", "-title", title, "-message", details);
}

Process process = builder.start();
try {
process.waitFor();
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // restore interrupted status
return;
}
}
}

/**
* Create a notification for GNU/Linux
*
* @param title the title of the notification.
* @param details the message of the notification.
* @param icon the icon to be displayed.
* @param timeout the duration after which the notification will be dismissed without the user intervention.
* @return the processbuilder for GNU/Linux
*/
private ProcessBuilder prepareLinuxNotifier(String title, String details, String icon, int timeout) {
if (Boolean.parseBoolean(System.getenv("KDE_FULL_SESSION"))) {
return new ProcessBuilder("/usr/bin/kdialog", "--passivepopup", details, "--title", title, "--icon", icon,
"" + timeout);
}
return new ProcessBuilder("/usr/bin/notify-send", title, details, "--icon=" + icon, "--hint=int:transient:1");
}

private enum OSName {

LINUX("Linux"), MAC("Mac OS X");

private String osName;

private OSName(String osName) {
this.osName = osName;
}

public static OSName getConstantForValue(String value) {
for (OSName constant : OSName.values()) {
if (constant.osName.equals(value)) {
return constant;
}
}
return null;
}

}
}
73 changes: 4 additions & 69 deletions src/main/java/ro/lmn/maven/mdn/NotifySendEventSpy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package ro.lmn.maven.mdn;

import java.io.IOException;
import java.util.List;

import org.apache.maven.eventspy.AbstractEventSpy;
Expand All @@ -27,14 +26,6 @@
@Component(role = EventSpy.class)
public class NotifySendEventSpy extends AbstractEventSpy {

private static final String ICON_INFO = "dialog-information";
private static final String ICON_ERROR = "dialog-error";

public static void main(String[] args) throws IOException {

new NotifySendEventSpy().notifySend("Test message", "This is an error!", ICON_ERROR);
}

@Override
public void onEvent(Object event) throws Exception {

Expand All @@ -51,71 +42,15 @@ public void onEvent(Object event) throws Exception {
String projectName = ee.getSession().getTopLevelProject().getName();
List<Throwable> exceptions = ee.getSession().getResult().getExceptions();

Notifier notifier = new Notifier();

if (exceptions == null || exceptions.isEmpty()) {
notifySend("Build successful", "Built " + projectName, ICON_INFO);
notifier.notifySend("Build successful", "Built " + projectName, Notifier.ICON_INFO);
} else {
String errorMessage = exceptions.get(0).getMessage();
notifySend("Build failed", projectName + " failed : " + errorMessage, ICON_ERROR);
notifier.notifySend("Build failed", projectName + " failed : " + errorMessage, Notifier.ICON_INFO);
}
}

private void notifySend(String title, String details, String icon) throws IOException {

ProcessBuilder builder = null;
OSName os = OSName.getConstantForValue(System.getProperty("os.name"));
if (os != null) {
switch (os) {
case LINUX:
builder = prepareLinuxNotifier(title, details, icon, 2);
break;
case MAC:
builder = new ProcessBuilder("terminal-notifier", "-title", title, "-message", details);
}

Process process = builder.start();
try {
process.waitFor();
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // restore interrupted status
return;
}
}
}

/**
* Create a notification for GNU/Linux
* @param title the title of the notification.
* @param details the message of the notification.
* @param icon the icon to be displayed.
* @param timeout the duration after which the notification will be dismissed without the user intervention.
* @return the processbuilder for GNU/Linux
*/
private ProcessBuilder prepareLinuxNotifier(String title, String details, String icon, int timeout) {
if(Boolean.parseBoolean(System.getenv("KDE_FULL_SESSION"))) {
return new ProcessBuilder("/usr/bin/kdialog", "--passivepopup", details , "--title", title, "--icon", icon, "" + timeout);
}
return new ProcessBuilder("/usr/bin/notify-send", title, details, "--icon=" + icon, "--hint=int:transient:1");
}

private enum OSName {

LINUX("Linux"),
MAC("Mac OS X");

private String osName;

private OSName(String osName) {
this.osName = osName;
}

public static OSName getConstantForValue(String value) {
for (OSName constant : OSName.values()) {
if (constant.osName.equals(value)) {
return constant;
}
}
return null;
}

}
}

0 comments on commit f88277a

Please sign in to comment.