PrepareFrontendMojo extends FlowModeAbstractMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { propagateBuildInfo(); final URI nodeDownloadRootURI; try { nodeDownloadRootURI = new URI(nodeDownloadRoot); } catch (URISyntaxException e) { throw new MojoExecutionException("Failed to parse " + nodeDownloadRoot, e); } try { FrontendTools tools = new FrontendTools(npmFolder.getAbsolutePath(), () -> FrontendUtils.getVaadinHomeDirectory().getAbsolutePath(), nodeVersion, nodeDownloadRootURI); tools.validateNodeAndNpmVersion(); } catch (IllegalStateException exception) { throw new MojoExecutionException(exception.getMessage(), exception); } try { FileUtils.forceMkdir(generatedFolder); } catch (IOException e) { throw new MojoFailureException("Failed to create folder '" + generatedFolder + "'. Verify that you may write to path.", e); } try { File flowResourcesFolder = new File(npmFolder, DEFAULT_FLOW_RESOURCES_FOLDER); NodeTasks.Builder builder = new NodeTasks.Builder( getClassFinder(project), npmFolder, generatedFolder, frontendDirectory) .withWebpack(webpackOutputDirectory, webpackTemplate, webpackGeneratedTemplate) .useV14Bootstrap(useDeprecatedV14Bootstrapping()) .withFlowResourcesFolder(flowResourcesFolder) .createMissingPackageJson(true) .enableImportsUpdate(false) .enablePackagesUpdate(false) .runNpmInstall(false) .withNodeVersion(nodeVersion) .withNodeDownloadRoot(nodeDownloadRootURI) .withHomeNodeExecRequired(requireHomeNodeExec); if ("jar".equals(project.getPackaging())) { Set<File> jarFiles = project.getArtifacts().stream() .filter(artifact -> "jar".equals(artifact.getType())) .map(Artifact::getFile).collect(Collectors.toSet()); builder.copyResources(jarFiles); } builder.build().execute(); } catch (ExecutionFailedException exception) { throw new MojoFailureException( "Could not execute prepare-frontend goal.", exception); } }  @Override void execute();  }
@Test public void tokenFileShouldExist_noDevModeTokenVisible() throws IOException, MojoExecutionException, MojoFailureException { mojo.execute(); Assert.assertTrue("No token file could be found", tokenFile.exists()); String json = org.apache.commons.io.FileUtils .readFileToString(tokenFile, "UTF-8"); JsonObject buildInfo = JsonUtil.parse(json); Assert.assertNull("No devMode token should be available", buildInfo.get(SERVLET_PARAMETER_ENABLE_DEV_SERVER)); Assert.assertNotNull("productionMode token should be available", buildInfo.get(SERVLET_PARAMETER_PRODUCTION_MODE)); Assert.assertNotNull( "useDeprecatedV14Bootstrapping token should be available", buildInfo.get(SERVLET_PARAMETER_USE_V14_BOOTSTRAP)); }
@Test public void existingTokenFile_enableDevServerShouldBeRemoved() throws IOException, MojoExecutionException, MojoFailureException { JsonObject initialBuildInfo = Json.createObject(); initialBuildInfo.put(SERVLET_PARAMETER_PRODUCTION_MODE, false); initialBuildInfo.put(SERVLET_PARAMETER_USE_V14_BOOTSTRAP, false); initialBuildInfo.put(SERVLET_PARAMETER_ENABLE_DEV_SERVER, false); org.apache.commons.io.FileUtils.forceMkdir(tokenFile.getParentFile()); org.apache.commons.io.FileUtils.write(tokenFile, JsonUtil.stringify(initialBuildInfo, 2) + "\n", "UTF-8"); mojo.execute(); String json = org.apache.commons.io.FileUtils .readFileToString(tokenFile, "UTF-8"); JsonObject buildInfo = JsonUtil.parse(json); Assert.assertNull("No devMode token should be available", buildInfo.get(SERVLET_PARAMETER_ENABLE_DEV_SERVER)); Assert.assertNotNull("productionMode token should be available", buildInfo.get(SERVLET_PARAMETER_PRODUCTION_MODE)); Assert.assertNotNull( "useDeprecatedV14Bootstrapping token should be available", buildInfo.get(SERVLET_PARAMETER_USE_V14_BOOTSTRAP)); }
@Test public void writeTokenFile_devModePropertiesAreWritten() throws IOException, MojoExecutionException, MojoFailureException { mojo.execute(); String json = org.apache.commons.io.FileUtils .readFileToString(tokenFile, StandardCharsets.UTF_8); JsonObject buildInfo = JsonUtil.parse(json); Assert.assertTrue( Constants.SERVLET_PARAMETER_ENABLE_PNPM + "should have been written", buildInfo.getBoolean(Constants.SERVLET_PARAMETER_ENABLE_PNPM)); Assert.assertTrue( Constants.REQUIRE_HOME_NODE_EXECUTABLE + "should have been written", buildInfo.getBoolean(Constants.REQUIRE_HOME_NODE_EXECUTABLE)); Assert.assertFalse(buildInfo .hasKey(Constants.SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE)); }
@Test public void mavenGoal_when_packageJsonMissing() throws Exception { Assert.assertFalse(FileUtils.fileExists(packageJson)); mojo.execute(); assertPackageJsonContent(); Assert.assertTrue(FileUtils.fileExists(webpackConfig)); }
@Test public void mavenGoal_when_packageWebpackConfigMissing() throws Exception { Assert.assertFalse(FileUtils.fileExists(webpackConfig)); mojo.execute(); Assert.assertTrue(FileUtils.fileExists(webpackConfig)); }
@Test public void should_keepDependencies_when_packageJsonExists() throws Exception { JsonObject json = TestUtils.getInitalPackageJson(); json.put("dependencies", Json.createObject()); json.getObject("dependencies").put("foo", "bar"); FileUtils.fileWrite(packageJson, json.toJson()); mojo.execute(); assertPackageJsonContent(); JsonObject packageJsonObject = getPackageJson(packageJson); assertContainsPackage(packageJsonObject.getObject("dependencies"), "foo"); }
@Test public void jarPackaging_copyProjectFrontendResources() throws MojoExecutionException, MojoFailureException, IllegalAccessException { Mockito.when(project.getPackaging()).thenReturn("jar"); ReflectionUtils.setVariableValueInObject(mojo, "project", project); mojo.execute(); Mockito.verify(project, Mockito.atLeastOnce()).getArtifacts(); }