CheMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { if (!srcJavaFolder.exists()) { Set<Artifact> artifacts = project.getDependencyArtifacts(); if (artifacts != null) { artifacts.stream().forEach(artifact -> archiver.addFile(artifact.getFile(), EXTENSIONS_DIRECTORY + "/" + artifact.getFile().getName()) ); } } else { archiver.addFile(compiledJarFile, EXTENSIONS_DIRECTORY + "/" + compiledJarFile.getName()); } File machinesFolder = new File(cheFolder, MACHINES_DIRECTORY); if (machinesFolder.exists()) { try { Files.walkFileTree(machinesFolder.toPath(), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (!attrs.isDirectory()) { String entryName = path.toFile().getAbsolutePath().substring(machinesFolder.getAbsolutePath().length()); archiver.addFile(path.toFile(), MACHINES_DIRECTORY + entryName); } return FileVisitResult.CONTINUE; } }); } catch (IOException e) { throw new MojoExecutionException("Cannot add machines", e); } } File templatesFolder = new File(cheFolder, TEMPLATES_DIRECTORY); if (templatesFolder.exists()) { try { Files.walkFileTree(templatesFolder.toPath(), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (!attrs.isDirectory()) { String entryName = path.toFile().getAbsolutePath().substring(templatesFolder.getAbsolutePath().length()); archiver.addFile(path.toFile(), TEMPLATES_DIRECTORY + entryName); } return FileVisitResult.CONTINUE; } }); } catch (IOException e) { throw new MojoExecutionException("Cannot add machines", e); } } if (!ymlFile.getParentFile().exists()) { if (!ymlFile.getParentFile().mkdirs()) { throw new MojoExecutionException("Unable to create parent directory '" + ymlFile.getParentFile() + "'."); } } try (Writer w = new OutputStreamWriter(new FileOutputStream(ymlFile), "UTF-8"); PrintWriter writer = new PrintWriter(w)) { writer.write("name: " + project.getArtifactId() + "\n"); writer.write("version: " + project.getVersion() + "\n"); archiver.addFile(ymlFile, ymlFile.getName()); } catch (IOException ioe) { throw new MojoExecutionException("Unable to write plugin metadata", ioe); } try { archiver.setDestFile(destFile); archiver.createArchive(); project.getArtifact().setFile(destFile); } catch (IOException e) { throw new MojoExecutionException("Cannot create plugin", e); } }  @Override void execute();  }
@Test public void testAssembly1() throws Exception { File projectCopy = this.resources.getBasedir("assembly1"); File pom = new File(projectCopy, "pom.xml"); Assert.assertNotNull(pom); Assert.assertTrue(pom.exists()); CheMojo mojo = (CheMojo)this.rule.lookupMojo("build", pom); configure(mojo, projectCopy); Assert.assertNotNull(mojo); mojo.execute(); boolean hasMachines = false; boolean hasTemplates = false; boolean hasExtensions = false; boolean hasPluginDescriptor = false; File destFile = (File)rule.getVariableValueFromObject(mojo, "destFile"); try (ZipFile zipFile = new ZipFile(destFile)) { Enumeration<? extends ZipEntry> zipEntries = zipFile.entries(); while (zipEntries.hasMoreElements()) { ZipEntry zipEntry = zipEntries.nextElement(); String entryName = zipEntry.getName(); if ("machines/machine1/Dockerfile".equals(entryName)) { hasMachines = true; } if ("templates/template1.json".equals(entryName)) { hasTemplates = true; } if ("extensions".equals(entryName)) { hasExtensions = true; } if ("che-plugin.yml".equals(entryName)) { hasPluginDescriptor = true; } } } Assert.assertTrue(hasPluginDescriptor); Assert.assertTrue(hasMachines); Assert.assertTrue(hasTemplates); Assert.assertFalse(hasExtensions); }
@Test public void testAssembly2() throws Exception { File projectCopy = this.resources.getBasedir("assembly2"); File pom = new File(projectCopy, "pom.xml"); Assert.assertNotNull(pom); Assert.assertTrue(pom.exists()); CheMojo mojo = (CheMojo)this.rule.lookupMojo("build", pom); configure(mojo, projectCopy); Assert.assertNotNull(mojo); mojo.execute(); boolean hasMachines = false; boolean hasTemplates = false; boolean hasExtensions = false; boolean hasPluginDescriptor = false; File destFile = (File)rule.getVariableValueFromObject(mojo, "destFile"); try (ZipFile zipFile = new ZipFile(destFile)) { Enumeration<? extends ZipEntry> zipEntries = zipFile.entries(); while (zipEntries.hasMoreElements()) { ZipEntry zipEntry = zipEntries.nextElement(); String entryName = zipEntry.getName(); if ("machines/machine1/Dockerfile".equals(entryName)) { hasMachines = true; } if ("templates/template1.json".equals(entryName)) { hasTemplates = true; } if ("extensions/junit.jar".equals(entryName)) { hasExtensions = true; } if ("che-plugin.yml".equals(entryName)) { hasPluginDescriptor = true; } } } Assert.assertTrue(hasPluginDescriptor); Assert.assertFalse(hasMachines); Assert.assertFalse(hasTemplates); Assert.assertTrue(hasExtensions); Assert.assertNotNull(mojo); mojo.execute(); }
@Test public void testSingle() throws Exception { File projectCopy = this.resources.getBasedir("single"); File pom = new File(projectCopy, "pom.xml"); Assert.assertNotNull(pom); Assert.assertTrue(pom.exists()); CheMojoProjectStubSingle project = new CheMojoProjectStubSingle(); String extensionName = project.getArtifactId() + "-" + project.getVersion() + ".jar"; CheMojo mojo = (CheMojo)this.rule.lookupMojo("build", pom); configure(mojo, projectCopy); this.rule.setVariableValueToObject(mojo, "compiledJarFile", new File(project.getOriginalBasedir(), File.separator + "target" + File.separator + "test-compilation" + File.separator + extensionName )); Assert.assertNotNull(mojo); mojo.execute(); boolean hasMachines = false; boolean hasTemplates = false; boolean hasExtensions = false; boolean hasPluginDescriptor = false; File destFile = (File)rule.getVariableValueFromObject(mojo, "destFile"); try (ZipFile zipFile = new ZipFile(destFile)) { Enumeration<? extends ZipEntry> zipEntries = zipFile.entries(); while (zipEntries.hasMoreElements()) { ZipEntry zipEntry = zipEntries.nextElement(); String entryName = zipEntry.getName(); if ("machines/machine1/Dockerfile".equals(entryName)) { hasMachines = true; } if ("templates/template1.json".equals(entryName)) { hasTemplates = true; } if (("extensions/" + extensionName).equals(entryName)) { hasExtensions = true; } if ("che-plugin.yml".equals(entryName)) { hasPluginDescriptor = true; } } } Assert.assertTrue(hasPluginDescriptor); Assert.assertTrue(hasMachines); Assert.assertTrue(hasTemplates); Assert.assertTrue(hasExtensions); }