diff --git a/.gitignore b/.gitignore index b25ab26..31ddcef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ nbactions.xml -release.sh \ No newline at end of file +release.sh +/target/ +/bin/ +.classpath +.project +.settings/ diff --git a/src/main/java/com/airhacks/wad/boundary/WADFlow.java b/src/main/java/com/airhacks/wad/boundary/WADFlow.java index d6cbc73..ba081bd 100644 --- a/src/main/java/com/airhacks/wad/boundary/WADFlow.java +++ b/src/main/java/com/airhacks/wad/boundary/WADFlow.java @@ -1,26 +1,29 @@ package com.airhacks.wad.boundary; -import com.airhacks.wad.control.Builder; -import com.airhacks.wad.control.Copier; -import com.airhacks.wad.control.FolderWatchService; -import com.airhacks.wad.control.TerminalColors; +import static java.time.temporal.ChronoField.HOUR_OF_DAY; +import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; +import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; + import java.io.IOException; import java.io.InputStream; import java.nio.file.Path; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; -import static java.time.temporal.ChronoField.HOUR_OF_DAY; -import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; -import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; import java.util.ArrayList; import java.util.List; import java.util.LongSummaryStatistics; import java.util.concurrent.atomic.AtomicLong; + import org.apache.maven.shared.invoker.InvocationResult; import org.apache.maven.shared.invoker.MavenInvocationException; +import com.airhacks.wad.control.Builder; +import com.airhacks.wad.control.Copier; +import com.airhacks.wad.control.FolderWatchService; +import com.airhacks.wad.control.TerminalColors; + /** * * @author airhacks.com @@ -34,111 +37,105 @@ public class WADFlow { private final Copier copier; private final Builder builder; - public WADFlow(Path dir, Path war, List deploymentTargets) throws IOException { - this.builder = new Builder(); - this.buildTimes = new ArrayList<>(); - this.copier = new Copier(war, deploymentTargets); - Runnable changeListener = this::buildAndDeploy; - changeListener.run(); - registerEnterListener(changeListener); - FolderWatchService.listenForChanges(dir, changeListener); + public WADFlow(Path dir, Path war, List deploymentTargets, Builder builder) { + this.builder = builder; + this.buildTimes = new ArrayList<>(); + this.copier = new Copier(war, deploymentTargets); + Runnable changeListener = this::buildAndDeploy; + changeListener.run(); + registerEnterListener(changeListener); + FolderWatchService.listenForChanges(dir, changeListener); } void registerEnterListener(Runnable listener) { - InputStream in = System.in; - Runnable task = () -> { - int c; - try { - while ((c = in.read()) != -1) { - listener.run(); - } - } catch (IOException ex) { - } - }; - new Thread(task).start(); + InputStream in = System.in; + Runnable task = () -> { + int c; + try { + while ((c = in.read()) != -1) { + listener.run(); + } + } catch (IOException ex) { + } + }; + new Thread(task).start(); } void buildAndDeploy() { - this.build(); - this.deploy(); + this.build(); + this.deploy(); } void build() { - long start = System.currentTimeMillis(); - try { - System.out.printf("[%s%s%s]", TerminalColors.TIME.value(), currentFormattedTime(), TerminalColors.RESET.value()); - InvocationResult result = this.builder.build(); - if (result.getExitCode() == 0) { - System.out.printf("[%d]", successCounter.incrementAndGet()); - System.out.print("\uD83D\uDC4D"); - long buildTime = (System.currentTimeMillis() - start); - buildTimes.add(buildTime); - System.out.println(" built in " + buildTime + " ms"); - if (buildTimes.size() % 10 == 0) { - this.printStatistics(); - } - } else { - System.out.printf("[%d] ", buildErrorCounter.incrementAndGet()); - System.out.println("\uD83D\uDC4E "); - } - } catch (MavenInvocationException ex) { - System.err.println(ex.getClass().getName() + " " + ex.getMessage()); - } + long start = System.currentTimeMillis(); + try { + System.out.printf("[%s%s%s]", TerminalColors.TIME.value(), currentFormattedTime(), + TerminalColors.RESET.value()); + InvocationResult result = this.builder.build(); + if (result.getExitCode() == 0) { + System.out.printf("[%d]", successCounter.incrementAndGet()); + System.out.print("\uD83D\uDC4D"); + long buildTime = System.currentTimeMillis() - start; + buildTimes.add(buildTime); + System.out.println(" built in " + buildTime + " ms"); + if (buildTimes.size() % 10 == 0) { + this.printStatistics(); + } + } else { + System.out.printf("[%d] ", buildErrorCounter.incrementAndGet()); + System.out.println("\uD83D\uDC4E "); + } + } catch (MavenInvocationException ex) { + System.err.println(ex.getClass().getName() + " " + ex.getMessage()); + } } void deploy() { - long start = System.currentTimeMillis(); - this.copier.copy(); - System.out.print("\uD83D\uDE80 "); - System.out.println(" copied in " + (System.currentTimeMillis() - start) + " ms"); + long start = System.currentTimeMillis(); + this.copier.copy(); + System.out.print("\uD83D\uDE80 "); + System.out.println(" copied in " + (System.currentTimeMillis() - start) + " ms"); } static String currentFormattedTime() { - DateTimeFormatter timeFormatter = new DateTimeFormatterBuilder() - .appendValue(HOUR_OF_DAY, 2) - .appendLiteral(':') - .appendValue(MINUTE_OF_HOUR, 2) - .optionalStart() - .appendLiteral(':') - .appendValue(SECOND_OF_MINUTE, 2) - .toFormatter(); - - return LocalTime.now().format(timeFormatter); + DateTimeFormatter timeFormatter = new DateTimeFormatterBuilder().appendValue(HOUR_OF_DAY, 2).appendLiteral(':') + .appendValue(MINUTE_OF_HOUR, 2).optionalStart().appendLiteral(':').appendValue(SECOND_OF_MINUTE, 2) + .toFormatter(); + + return LocalTime.now().format(timeFormatter); } public LongSummaryStatistics buildTimeStatistics() { - return this.buildTimes. - stream(). - mapToLong(t -> t). - summaryStatistics(); + return this.buildTimes.stream().mapToLong(t -> t).summaryStatistics(); } String statisticsSummary() { - LongSummaryStatistics warSizeStatistics = this.copier.warSizeStatistics(); - long maxKb = warSizeStatistics.getMax(); - long minKb = warSizeStatistics.getMin(); - long totalKb = warSizeStatistics.getSum(); - String warStats = String.format("WAR sizes: min %d kB, max %d kB, total %d kB\n", minKb, maxKb, totalKb); - - LongSummaryStatistics buildTimeStatistics = this.buildTimeStatistics(); - long maxTime = buildTimeStatistics.getMax(); - long minTime = buildTimeStatistics.getMin(); - long totalTime = buildTimeStatistics.getSum(); - String buildTimeStats = String.format("Build times: min %d ms, max %d ms, total %d ms\n", minTime, maxTime, totalTime); - - String failureStats; - long failedBuilds = buildErrorCounter.get(); - if (failedBuilds == 0) { - failureStats = "Great! Every build was a success!"; - } else { - failureStats = String.format("%d builds failed", buildErrorCounter.get()); - } - return warStats + buildTimeStats + failureStats; + LongSummaryStatistics warSizeStatistics = this.copier.warSizeStatistics(); + long maxKb = warSizeStatistics.getMax(); + long minKb = warSizeStatistics.getMin(); + long totalKb = warSizeStatistics.getSum(); + String warStats = String.format("WAR sizes: min %d kB, max %d kB, total %d kB\n", minKb, maxKb, totalKb); + + LongSummaryStatistics buildTimeStatistics = this.buildTimeStatistics(); + long maxTime = buildTimeStatistics.getMax(); + long minTime = buildTimeStatistics.getMin(); + long totalTime = buildTimeStatistics.getSum(); + String buildTimeStats = String.format("Build times: min %d ms, max %d ms, total %d ms\n", minTime, maxTime, + totalTime); + + String failureStats; + long failedBuilds = buildErrorCounter.get(); + if (failedBuilds == 0) { + failureStats = "Great! Every build was a success!"; + } else { + failureStats = String.format("%d builds failed", buildErrorCounter.get()); + } + return warStats + buildTimeStats + failureStats; } void printStatistics() { - System.out.println(statisticsSummary()); + System.out.println(statisticsSummary()); } } diff --git a/src/main/java/com/airhacks/wad/control/Builder.java b/src/main/java/com/airhacks/wad/control/Builder.java index 8ecf589..6838903 100644 --- a/src/main/java/com/airhacks/wad/control/Builder.java +++ b/src/main/java/com/airhacks/wad/control/Builder.java @@ -1,44 +1,14 @@ - package com.airhacks.wad.control; -import com.airhacks.wad.control.SilentLogger; -import java.io.File; -import java.util.Arrays; -import java.util.List; -import java.util.Properties; -import org.apache.maven.shared.invoker.DefaultInvocationRequest; -import org.apache.maven.shared.invoker.DefaultInvoker; +import java.nio.file.Path; + import org.apache.maven.shared.invoker.InvocationResult; import org.apache.maven.shared.invoker.MavenInvocationException; -/** - * - * @author airhacks.com - */ -public class Builder { - - private final DefaultInvoker invoker; - private final DefaultInvocationRequest request; +public interface Builder { - public Builder() { - this.invoker = new DefaultInvoker(); - this.invoker.setLogger(new SilentLogger()); - this.invoker.setOutputHandler((line) -> { - }); - List goals = Arrays.asList("clean", "package"); - Properties properties = new Properties(); - properties.put("maven.test.skip", String.valueOf(true)); - this.request = new DefaultInvocationRequest(); - this.request.setPomFile(new File("./pom.xml")); - this.request.setGoals(goals); - this.request.setBatchMode(true); - this.request.setProperties(properties); - this.request.setThreads(System.getProperty("threads", "1")); - this.request.setShowErrors(true); - } + InvocationResult build() throws MavenInvocationException; - public InvocationResult build() throws MavenInvocationException { - return this.invoker.execute(request); - } + Path getThinWarPath(); -} +} \ No newline at end of file diff --git a/src/main/java/com/airhacks/wad/control/Copier.java b/src/main/java/com/airhacks/wad/control/Copier.java index 2fe1f0e..caebf62 100644 --- a/src/main/java/com/airhacks/wad/control/Copier.java +++ b/src/main/java/com/airhacks/wad/control/Copier.java @@ -15,47 +15,44 @@ */ public class Copier { - private List warSizes; + private final List warSizes; private final List deploymentTargets; private final Path from; public Copier(Path from, List deploymentTargets) { - this.warSizes = new ArrayList<>(); - this.from = from; - this.deploymentTargets = deploymentTargets; + this.warSizes = new ArrayList<>(); + this.from = from; + this.deploymentTargets = deploymentTargets; } - - String shortenForDisplay(Path path, int maxLength) { - String message = path.toString(); - int length = message.length(); - if (length > maxLength) { - return "(...)" + message.substring(length - maxLength); - } else { - return message; - } + String message = path.toString(); + int length = message.length(); + if (length > maxLength) + return "(...)" + message.substring(length - maxLength); + return message; } public void copy() { - deploymentTargets.forEach(target -> copySingle(this.from, target)); + deploymentTargets.forEach(target -> copySingle(this.from, target)); } Path copySingle(Path from, Path to) { - long kb; - try { - kb = Files.size(from) / 1024; - warSizes.add(kb); - System.out.printf("Copying %dkB ThinWAR to %s %s %s \n", kb, TerminalColors.FILE.value(), shortenForDisplay(to, 40), TerminalColors.RESET.value()); - return Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING); - - } catch (IOException ex) { - throw new IllegalStateException(ex.getMessage(), ex); - } + long kb; + try { + kb = Files.size(from) / 1024; + warSizes.add(kb); + System.out.printf("Copying %dkB ThinWAR to %s %s %s \n", kb, TerminalColors.FILE.value(), + shortenForDisplay(to, 40), TerminalColors.RESET.value()); + return Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING); + + } catch (IOException ex) { + throw new IllegalStateException(ex.getMessage(), ex); + } } public LongSummaryStatistics warSizeStatistics() { - return this.warSizes.stream().mapToLong(s -> s).summaryStatistics(); + return this.warSizes.stream().mapToLong(s -> s).summaryStatistics(); } } diff --git a/src/main/java/com/airhacks/wad/control/FolderWatchService.java b/src/main/java/com/airhacks/wad/control/FolderWatchService.java index 9a2b753..ffac6bb 100644 --- a/src/main/java/com/airhacks/wad/control/FolderWatchService.java +++ b/src/main/java/com/airhacks/wad/control/FolderWatchService.java @@ -19,60 +19,60 @@ public interface FolderWatchService { static long POLLING_INTERVALL = 500; static String POM = "pom.xml"; + static String GRADLE = "build.gradle"; - public static void listenForChanges(Path dir, Runnable listener) throws IOException { - ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); - checkForChanges(scheduler, dir, listener); + public static void listenForChanges(Path dir, Runnable listener) { + ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + checkForChanges(scheduler, dir, listener); } static void checkForChanges(ScheduledExecutorService scheduler, Path dir, Runnable changeListener) { - long initialStamp = getProjectModificationId(dir); - boolean changeDetected = false; - while (true) { - try { - final long previous = initialStamp; - changeDetected = scheduler. - schedule(() -> detectModification(dir, previous), POLLING_INTERVALL, TimeUnit.MILLISECONDS). - get(); - } catch (InterruptedException | ExecutionException ex) { - throw new IllegalStateException("Scheduler error", ex); - } - if (changeDetected) { - changeListener.run(); - initialStamp = getProjectModificationId(dir); - } - } + long initialStamp = getProjectModificationId(dir); + boolean changeDetected = false; + while (true) { + try { + final long previous = initialStamp; + changeDetected = scheduler + .schedule(() -> detectModification(dir, previous), POLLING_INTERVALL, TimeUnit.MILLISECONDS) + .get(); + } catch (InterruptedException | ExecutionException ex) { + throw new IllegalStateException("Scheduler error", ex); + } + if (changeDetected) { + changeListener.run(); + initialStamp = getProjectModificationId(dir); + } + } } static long getPomModificationStamp() { - return getFileSize(Paths.get(POM)); + return getFileSize(Paths.get(POM)) + getFileSize(Paths.get(GRADLE)); } static boolean detectModification(Path dir, long previousStamp) { - long currentStamp = getProjectModificationId(dir); - return previousStamp != currentStamp; + long currentStamp = getProjectModificationId(dir); + return previousStamp != currentStamp; } static long getProjectModificationId(Path dir) { - try { - long modificationId = Files.walk(dir). - filter(Files::isRegularFile). - mapToLong(FolderWatchService::getFileSize). - sum(); - modificationId += getPomModificationStamp(); - return modificationId; - } catch (IOException ex) { - throw new IllegalStateException("Cannot list files", ex); - } + try { + long modificationId = Files.walk(dir).filter(Files::isRegularFile) + .mapToLong(FolderWatchService::getFileSize).sum(); + modificationId += getPomModificationStamp(); + return modificationId; + } catch (IOException ex) { + throw new IllegalStateException("Cannot list files", ex); + } } static long getFileSize(Path p) { - try { - return Files.size(p); - } catch (IOException ex) { - throw new IllegalStateException("Cannot obtain FileTime", ex); - } + try { + if (Files.exists(p)) + return Files.size(p); + return 0; + } catch (IOException ex) { + throw new IllegalStateException("Cannot obtain FileTime", ex); + } } - } diff --git a/src/main/java/com/airhacks/wad/control/GradleBuilder.java b/src/main/java/com/airhacks/wad/control/GradleBuilder.java new file mode 100644 index 0000000..7f72f04 --- /dev/null +++ b/src/main/java/com/airhacks/wad/control/GradleBuilder.java @@ -0,0 +1,49 @@ +package com.airhacks.wad.control; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.apache.maven.shared.invoker.InvocationResult; +import org.apache.maven.shared.invoker.MavenInvocationException; +import org.apache.maven.shared.utils.cli.CommandLineException; + +public class GradleBuilder implements Builder { + + @Override + public InvocationResult build() throws MavenInvocationException { + try { + int exitCode = new ProcessBuilder("./gradlew", "war").start().waitFor(); + return new InvocationResult() { + + @Override + public int getExitCode() { + return exitCode; + } + + @Override + public CommandLineException getExecutionException() { + return null; + } + }; + } catch (IOException | InterruptedException e) { + throw new MavenInvocationException("Gradle build failed", e); + } + } + + @Override + public Path getThinWarPath() { + try { + build(); + } catch (MavenInvocationException e1) { + // can be ignored + } + Path thinWARPath = Paths.get("build").resolve("libs"); + try { + return Files.find(thinWARPath, 1, (p, a) -> p.toString().endsWith(".war")).findFirst().orElse(thinWARPath); + } catch (IOException e) { + return thinWARPath; + } + } +} diff --git a/src/main/java/com/airhacks/wad/control/MavenBuilder.java b/src/main/java/com/airhacks/wad/control/MavenBuilder.java new file mode 100644 index 0000000..06d410d --- /dev/null +++ b/src/main/java/com/airhacks/wad/control/MavenBuilder.java @@ -0,0 +1,56 @@ + +package com.airhacks.wad.control; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; + +import org.apache.maven.shared.invoker.DefaultInvocationRequest; +import org.apache.maven.shared.invoker.DefaultInvoker; +import org.apache.maven.shared.invoker.InvocationResult; +import org.apache.maven.shared.invoker.MavenInvocationException; + +/** + * + * @author airhacks.com + */ +public class MavenBuilder implements Builder { + + private final DefaultInvoker invoker; + private final DefaultInvocationRequest request; + + public MavenBuilder() { + this.invoker = new DefaultInvoker(); + this.invoker.setLogger(new SilentLogger()); + this.invoker.setOutputHandler((line) -> { + }); + List goals = Arrays.asList("clean", "package"); + Properties properties = new Properties(); + properties.put("maven.test.skip", String.valueOf(true)); + this.request = new DefaultInvocationRequest(); + this.request.setPomFile(new File("./pom.xml")); + this.request.setGoals(goals); + this.request.setBatchMode(true); + this.request.setProperties(properties); + this.request.setThreads(System.getProperty("threads", "1")); + this.request.setShowErrors(true); + } + + @Override + public InvocationResult build() throws MavenInvocationException { + return this.invoker.execute(request); + } + + @Override + public Path getThinWarPath() { + Path currentPath = Paths.get("").toAbsolutePath(); + Path currentDirectory = currentPath.getFileName(); + String thinWARName = currentDirectory + ".war"; + + Path thinWARPath = Paths.get("target", thinWARName); + return thinWARPath; + } +} diff --git a/src/main/java/com/airhacks/wad/control/PreBuildChecks.java b/src/main/java/com/airhacks/wad/control/PreBuildChecks.java index 3010aa7..6d9d1ae 100644 --- a/src/main/java/com/airhacks/wad/control/PreBuildChecks.java +++ b/src/main/java/com/airhacks/wad/control/PreBuildChecks.java @@ -17,57 +17,61 @@ */ public interface PreBuildChecks { - static void pomExists() { - Path pomPath = Paths.get("pom.xml"); - if (!Files.exists(pomPath)) { - printUsage(); - exit(); - } + static Builder getBuilder() { + if (fileExists("pom.xml")) + return new MavenBuilder(); + if (fileExists("build.gradle")) + return new GradleBuilder(); + printUsage(); + exit(); + return null; + } + + static boolean fileExists(String buildFile) { + Path buildfilePath = Paths.get(buildFile); + return Files.exists(buildfilePath); } static void exit() { - System.exit(-1); + System.exit(-1); } public static void printUsage() { - String message = loadMessage("usage.txt"); - System.out.println(message); + String message = loadMessage("usage.txt"); + System.out.println(message); } public static String loadMessage(String fileName) { - InputStream stream = PreBuildChecks.class.getResourceAsStream("/" + fileName); - return load(stream); + InputStream stream = PreBuildChecks.class.getResourceAsStream("/" + fileName); + return load(stream); } static String load(InputStream stream) { - try ( BufferedReader buffer = new BufferedReader(new InputStreamReader(stream))) { - return buffer.lines().collect(Collectors.joining("\n")); - } catch (IOException ex) { - throw new IllegalStateException("Cannot read from stream", ex); - } + try (BufferedReader buffer = new BufferedReader(new InputStreamReader(stream))) { + return buffer.lines().collect(Collectors.joining("\n")); + } catch (IOException ex) { + throw new IllegalStateException("Cannot read from stream", ex); + } } static void validateDeploymentDirectories(Set path) { - long invalidDirectories = path.stream(). - map(PreBuildChecks::validateDeploymentDirectory). - filter(valid -> valid == false). - count(); - if (invalidDirectories != 0) { - exit(); - } + long invalidDirectories = path.stream().map(PreBuildChecks::validateDeploymentDirectory) + .filter(valid -> valid == false).count(); + if (invalidDirectories != 0) { + exit(); + } } static boolean validateDeploymentDirectory(Path path) { - if (!Files.exists(path)) { - System.err.printf("Directory \'%s\' does not exist\n", path); - return false; - } - if (!Files.isDirectory(path)) { - System.err.printf("%s is not a directory", path); - return false; - } - return true; + if (!Files.exists(path)) { + System.err.printf("Directory \'%s\' does not exist\n", path); + return false; + } + if (!Files.isDirectory(path)) { + System.err.printf("%s is not a directory", path); + return false; + } + return true; } - } diff --git a/src/main/java/wad/App.java b/src/main/java/wad/App.java index e013a1c..8b85bff 100644 --- a/src/main/java/wad/App.java +++ b/src/main/java/wad/App.java @@ -1,10 +1,9 @@ package wad; -import com.airhacks.wad.boundary.WADFlow; -import com.airhacks.wad.control.Configurator; -import static com.airhacks.wad.control.PreBuildChecks.pomExists; +import static com.airhacks.wad.control.PreBuildChecks.getBuilder; import static com.airhacks.wad.control.PreBuildChecks.validateDeploymentDirectories; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -16,63 +15,58 @@ import java.util.Set; import java.util.stream.Collectors; +import com.airhacks.wad.boundary.WADFlow; +import com.airhacks.wad.control.Builder; +import com.airhacks.wad.control.Configurator; + /** * * @author airhacks.com */ public class App { - + static Path addTrailingSlash(String path) { - if (!path.endsWith(File.separator)) { - return Paths.get(path, File.separator); - } - return Paths.get(path); + if (!path.endsWith(File.separator)) + return Paths.get(path, File.separator); + return Paths.get(path); } static void printWelcomeMessage() throws IOException { - try (InputStream resourceAsStream = App.class. - getClassLoader(). - getResourceAsStream("META-INF/maven/com.airhacks/wad/pom.properties")) { - Properties properties = new Properties(); - properties.load(resourceAsStream); - String wad = properties.getProperty("artifactId"); - String version = properties.getProperty("version"); - System.out.println(wad + " " + version); - } + try (InputStream resourceAsStream = App.class.getClassLoader() + .getResourceAsStream("META-INF/maven/com.airhacks/wad/pom.properties")) { + Properties properties = new Properties(); + properties.load(resourceAsStream); + String wad = properties.getProperty("artifactId"); + String version = properties.getProperty("version"); + System.out.println(wad + " " + version); + } } static List convert(String[] args) { - return Arrays.stream(args).map(App::addTrailingSlash).collect(Collectors.toList()); + return Arrays.stream(args).map(App::addTrailingSlash).collect(Collectors.toList()); } - static List addWarName(Set deploymentDirectories, String warName) { - return deploymentDirectories. - stream(). - map(path -> path.resolve(warName)). - collect(Collectors.toList()); + static List addWarName(Set deploymentDirectories, Path warName) { + return deploymentDirectories.stream().map(path -> path.resolve(warName.getFileName())) + .collect(Collectors.toList()); } - public static void main(String[] args) throws IOException { - printWelcomeMessage(); - if (args.length < 1 && !Configurator.userConfigurationExists()) { - System.out.println("Invoke with java -jar wad.jar [DEPLOYMENT_DIR1,DEPLOYMENT_DIR1] or create ~/.wadrc"); - System.exit(-1); - } - pomExists(); - Path currentPath = Paths.get("").toAbsolutePath(); - Path currentDirectory = currentPath.getFileName(); - String thinWARName = currentDirectory + ".war"; - - Path thinWARPath = Paths.get("target", thinWARName); + printWelcomeMessage(); + if (args.length < 1 && !Configurator.userConfigurationExists()) { + System.out.println("Invoke with java -jar wad.jar [DEPLOYMENT_DIR1,DEPLOYMENT_DIR1] or create ~/.wadrc"); + System.exit(-1); + } + Builder builder = getBuilder(); + Path thinWARPath = builder.getThinWarPath(); - Set deploymentDirs = Configurator.getConfiguredFolders(convert(args)); - validateDeploymentDirectories(deploymentDirs); + Set deploymentDirs = Configurator.getConfiguredFolders(convert(args)); + validateDeploymentDirectories(deploymentDirs); - List deploymentTargets = addWarName(deploymentDirs, thinWARName); - Path sourceCodeDir = Paths.get("./src/main/"); - System.out.printf("WAD is watching %s, deploying %s to %s \n", sourceCodeDir, thinWARPath, deploymentTargets); - WADFlow wadFlow = new WADFlow(sourceCodeDir, thinWARPath, deploymentTargets); + List deploymentTargets = addWarName(deploymentDirs, thinWARPath); + Path sourceCodeDir = Paths.get("./src/main/"); + System.out.printf("WAD is watching %s, deploying %s to %s \n", sourceCodeDir, thinWARPath, deploymentTargets); + WADFlow wadFlow = new WADFlow(sourceCodeDir, thinWARPath, deploymentTargets, builder); } } diff --git a/src/test/java/com/airhacks/wad/control/FolderWatchServiceTest.java b/src/test/java/com/airhacks/wad/control/FolderWatchServiceTest.java index 6045c18..235af1a 100644 --- a/src/test/java/com/airhacks/wad/control/FolderWatchServiceTest.java +++ b/src/test/java/com/airhacks/wad/control/FolderWatchServiceTest.java @@ -1,5 +1,8 @@ package com.airhacks.wad.control; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -7,9 +10,8 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.not; + import org.junit.Assert; -import static org.junit.Assert.assertThat; import org.junit.Before; import org.junit.Test; @@ -19,66 +21,65 @@ public class FolderWatchServiceTest { private List tempFiles; @Before - public void init() throws IOException { - this.testFolderPath = Paths.get("target"); + public void init() { + this.testFolderPath = Paths.get("target"); - this.tempFiles = Stream.generate(this::createFile). - limit(15). - collect(Collectors.toList()); + this.tempFiles = Stream.generate(this::createFile).limit(15).collect(Collectors.toList()); } Path createFile() { - try { - Path p = Files.createTempFile(this.testFolderPath, "wad", "java"); - String fileContent = "duke" + System.nanoTime(); - Files.write(p, fileContent.getBytes()); - return p; - } catch (IOException ex) { - throw new IllegalStateException("temp file creation", ex); - } + try { + Path p = Files.createTempFile(this.testFolderPath, "wad", "java"); + String fileContent = "duke" + System.nanoTime(); + Files.write(p, fileContent.getBytes()); + return p; + } catch (IOException ex) { + throw new IllegalStateException("temp file creation", ex); + } } void deleteFile(int index) throws IOException { - Path file = this.tempFiles.get(index); - Files.delete(file); - this.tempFiles.remove(file); + Path file = this.tempFiles.get(index); + Files.delete(file); + this.tempFiles.remove(file); } void changeFile(int index, String content) throws IOException { - Files.write(this.tempFiles.get(index), content.getBytes()); + Files.write(this.tempFiles.get(index), content.getBytes()); } void addFile() { - this.tempFiles.add(createFile()); + this.tempFiles.add(createFile()); } @Test - public void stampsAreEqualWithoutModification() throws IOException { - long stamp = FolderWatchService.getProjectModificationId(this.testFolderPath); - long next = FolderWatchService.getProjectModificationId(this.testFolderPath); - Assert.assertEquals(stamp, next); + public void stampsAreEqualWithoutModification() { + long stamp = FolderWatchService.getProjectModificationId(this.testFolderPath); + long next = FolderWatchService.getProjectModificationId(this.testFolderPath); + Assert.assertEquals(stamp, next); } @Test - public void addingChangesStamp() throws IOException { - long stamp = FolderWatchService.getProjectModificationId(this.testFolderPath); - addFile(); - long next = FolderWatchService.getProjectModificationId(this.testFolderPath); - assertThat(stamp, not(next)); + public void addingChangesStamp() { + long stamp = FolderWatchService.getProjectModificationId(this.testFolderPath); + addFile(); + long next = FolderWatchService.getProjectModificationId(this.testFolderPath); + assertThat(stamp, not(next)); } @Test public void changingFileChangesStamp() throws IOException { - long stamp = FolderWatchService.getProjectModificationId(this.testFolderPath); - changeFile(1, "Java EE rocks"); - long next = FolderWatchService.getProjectModificationId(this.testFolderPath); - assertThat(stamp, not(next)); + long stamp = FolderWatchService.getProjectModificationId(this.testFolderPath); + changeFile(1, "Java EE rocks"); + long next = FolderWatchService.getProjectModificationId(this.testFolderPath); + assertThat(stamp, not(next)); } + @Test public void deletingFileChangesStamp() throws IOException { - long stamp = FolderWatchService.getProjectModificationId(this.testFolderPath); - deleteFile(2); - long next = FolderWatchService.getProjectModificationId(this.testFolderPath); - assertThat(stamp, not(next)); + long stamp = FolderWatchService.getProjectModificationId(this.testFolderPath); + deleteFile(2); + long next = FolderWatchService.getProjectModificationId(this.testFolderPath); + assertThat(stamp, not(next)); } }