NinjaRunMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException { if (skip) { getLog().info("Skip flag is on. Will not execute."); return; } initMojoFromUserSubmittedParameters(); List<String> classpathItems = new ArrayList<>(); alertAndStopExecutionIfDirectoryWithCompiledClassesOfThisProjectDoesNotExist( buildOutputDirectory); classpathItems.add(buildOutputDirectory); for (org.apache.maven.artifact.Artifact artifact : project.getArtifacts()) { classpathItems.add(artifact.getFile().toString()); } List<Artifact> allArtifactsFromNinjaStandaloneInPlugin = getAllArtifactsComingFromNinjaStandalone(pluginArtifacts); for (Artifact artifact: allArtifactsFromNinjaStandaloneInPlugin) { if (!classpathItems.contains(artifact.getFile().toString())) { classpathItems.add(artifact.getFile().toString()); } } Set<String> includesAsSet = new LinkedHashSet<>((includes != null ? includes : Collections.EMPTY_LIST)); Set<String> excludesAsSet = new LinkedHashSet<>((excludes != null ? excludes : Collections.EMPTY_LIST)); Set<Path> directoriesToRecursivelyWatch = new LinkedHashSet<>(); directoriesToRecursivelyWatch.add( FileSystems.getDefault().getPath( buildOutputDirectory).toAbsolutePath()); if (this.watchDirs != null) { for (File watchDir: this.watchDirs) { directoriesToRecursivelyWatch.add(watchDir.toPath().toAbsolutePath()); } } for (org.apache.maven.artifact.Artifact artifact : project.getArtifacts()) { File file = artifact.getFile(); if (file.isDirectory() && this.watchAllClassPathDirs) { directoriesToRecursivelyWatch.add(file.toPath().toAbsolutePath()); } else if (file.getName().endsWith(".jar") && this.watchAllClassPathJars) { File parentDir = file.getParentFile(); Path parentPath = parentDir.toPath().toAbsolutePath(); String rulePrefix = parentDir.getAbsolutePath() + File.separator; rulePrefix = rulePrefix.replace("\\", "\\\\"); if (!directoriesToRecursivelyWatch.contains(parentPath)) { excludesAsSet.add(rulePrefix + "(.*)$"); } includesAsSet.add(rulePrefix + file.getName() + "$"); directoriesToRecursivelyWatch.add(parentPath); } } String mainClassToRun = (mainClass != null ? mainClass : AutoStandalone.class.getCanonicalName()); getLog().info("------------------------------------------------------------------------"); getLog().info("Ninja will watch dirs:"); for (Path path : directoriesToRecursivelyWatch) { getLog().info(" " + path); } getLog().info(""); getLog().info("Ninja will launch with:"); getLog().info(" context: " + (getContextPath() != null ? getContextPath() : "<default>")); getLog().info(" mode: " + mode); getLog().info(" port: " + (port != null ? port : "<default>")); getLog().info(" ssl port: " + (sslPort != null ? sslPort : "<default>")); getLog().info(" mainClass: " + mainClassToRun); getLog().info("extra jvmArgs: " + (jvmArgs != null ? jvmArgs : "<none>")); getLog().info("------------------------------------------------------------------------"); try { RunClassInSeparateJvmMachine machine = buildRunClassInSeparateJvmMachine( "Standalone", mainClassToRun, classpathItems, buildJvmArguments(), project.getBasedir() ); DelayedRestartTrigger restartTrigger = buildDelayedRestartTrigger(machine); restartTrigger.setSettleDownMillis(settleDownMillis); restartTrigger.start(); WatchAndRestartMachine watcher = buildWatchAndRestartMachine( directoriesToRecursivelyWatch, includesAsSet, excludesAsSet, restartTrigger); machine.restart(); watcher.run(); } catch (IOException e) { getLog().error(e); } }  @Override void execute();  }
@Test public void minimal() throws Exception { ninjaRunMojo.buildOutputDirectory = "target/classes"; ninjaRunMojo.project = project; ninjaRunMojo.pluginArtifacts = new ArrayList<>(); ninjaRunMojo.settleDownMillis = 500L; ninjaRunMojo.mode = "dev"; setupMachineAndWatcherStubs(); ninjaRunMojo.execute(); verify(machine, times(1)).restart(); verify(watcher, times(1)).run(); assertThat(jvmArgumentsCaptor.getValue().get(0), is("-Dninja.mode=dev")); }
@Test public void portProperty() throws Exception { ninjaRunMojo.buildOutputDirectory = "target/classes"; ninjaRunMojo.project = project; ninjaRunMojo.pluginArtifacts = new ArrayList<>(); ninjaRunMojo.settleDownMillis = 500L; ninjaRunMojo.mode = "dev"; ninjaRunMojo.port = 9000; setupMachineAndWatcherStubs(); ninjaRunMojo.execute(); verify(machine, times(1)).restart(); verify(watcher, times(1)).run(); assertThat(jvmArgumentsCaptor.getValue().get(0), is("-Dninja.mode=dev")); assertThat(jvmArgumentsCaptor.getValue().get(1), is("-Dninja.port=9000")); }
@Test public void contextProperty() throws Exception { ninjaRunMojo.buildOutputDirectory = "target/classes"; ninjaRunMojo.project = project; ninjaRunMojo.pluginArtifacts = new ArrayList<>(); ninjaRunMojo.settleDownMillis = 500L; ninjaRunMojo.mode = "dev"; ninjaRunMojo.context = "/test"; setupMachineAndWatcherStubs(); ninjaRunMojo.execute(); verify(machine, times(1)).restart(); verify(watcher, times(1)).run(); assertThat(jvmArgumentsCaptor.getValue().get(0), is("-Dninja.mode=dev")); assertThat(jvmArgumentsCaptor.getValue().get(1), is("-Dninja.context=/test")); }
@Test public void contextPropertyFallbackToContextPath() throws Exception { ninjaRunMojo.buildOutputDirectory = "target/classes"; ninjaRunMojo.project = project; ninjaRunMojo.pluginArtifacts = new ArrayList<>(); ninjaRunMojo.settleDownMillis = 500L; ninjaRunMojo.mode = "dev"; ninjaRunMojo.contextPath = "/test"; setupMachineAndWatcherStubs(); ninjaRunMojo.execute(); verify(machine, times(1)).restart(); verify(watcher, times(1)).run(); assertThat(jvmArgumentsCaptor.getValue().get(0), is("-Dninja.mode=dev")); assertThat(jvmArgumentsCaptor.getValue().get(1), is("-Dninja.context=/test")); }
@Test public void mainClassProperty() throws Exception { ninjaRunMojo.buildOutputDirectory = "target/classes"; ninjaRunMojo.project = project; ninjaRunMojo.pluginArtifacts = new ArrayList<>(); ninjaRunMojo.settleDownMillis = 500L; ninjaRunMojo.mode = "dev"; ninjaRunMojo.mainClass = "this.is.a.class.to.run"; setupMachineAndWatcherStubs(); ninjaRunMojo.execute(); verify(machine, times(1)).restart(); verify(watcher, times(1)).run(); assertThat(classNameWithMainToRunCaptor.getValue(), is("this.is.a.class.to.run")); }
@Test public void jvmArgsProperty() throws Exception { ninjaRunMojo.buildOutputDirectory = "target/classes"; ninjaRunMojo.project = project; ninjaRunMojo.pluginArtifacts = new ArrayList<>(); ninjaRunMojo.settleDownMillis = 500L; ninjaRunMojo.mode = "dev"; ninjaRunMojo.jvmArgs = "\"This has spaces\" -Dprop=name -XXXX"; setupMachineAndWatcherStubs(); ninjaRunMojo.execute(); verify(machine, times(1)).restart(); verify(watcher, times(1)).run(); ArrayList<String> value = jvmArgumentsCaptor.getValue(); assertThat(value.get(0), is("-Dninja.mode=dev")); assertThat(value.get(1), is("This has spaces")); assertThat(value.get(2), is("-Dprop=name")); assertThat(value.get(3), is("-XXXX")); }