diff --git a/pom.xml b/pom.xml index a51d0dc..3512efb 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,18 @@ + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + ro.lmn.maven.mdn.Main + + + + org.apache.maven.plugins maven-compiler-plugin diff --git a/src/main/java/ro/lmn/maven/mdn/Main.java b/src/main/java/ro/lmn/maven/mdn/Main.java new file mode 100644 index 0000000..4656c40 --- /dev/null +++ b/src/main/java/ro/lmn/maven/mdn/Main.java @@ -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); + } + + } + +} diff --git a/src/main/java/ro/lmn/maven/mdn/Notifier.java b/src/main/java/ro/lmn/maven/mdn/Notifier.java new file mode 100644 index 0000000..38072fb --- /dev/null +++ b/src/main/java/ro/lmn/maven/mdn/Notifier.java @@ -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; + } + + } +} diff --git a/src/main/java/ro/lmn/maven/mdn/NotifySendEventSpy.java b/src/main/java/ro/lmn/maven/mdn/NotifySendEventSpy.java index 49b0931..c12a1b7 100644 --- a/src/main/java/ro/lmn/maven/mdn/NotifySendEventSpy.java +++ b/src/main/java/ro/lmn/maven/mdn/NotifySendEventSpy.java @@ -15,7 +15,6 @@ */ package ro.lmn.maven.mdn; -import java.io.IOException; import java.util.List; import org.apache.maven.eventspy.AbstractEventSpy; @@ -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 { @@ -51,71 +42,15 @@ public void onEvent(Object event) throws Exception { String projectName = ee.getSession().getTopLevelProject().getName(); List 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; - } - - } }