use of org.apache.camel.maven.packaging.ComponentDslMojo in project cq-maven-plugin by l2x6.
the class CamelProdExcludesMojo method handleExcludedTargetDirectories.
void handleExcludedTargetDirectories(final Path basePath, final MavenSourceTree fullTree, final Set<Ga> excludes, Predicate<Profile> profiles) {
/* Clean the target folders in all excluded modules so that Camel plugins do not see any stale content there */
excludes.stream().map(ga -> fullTree.getModulesByGa().get(ga)).map(Module::getPomPath).map(basePath::resolve).map(Path::getParent).map(absPath -> absPath.resolve("target")).filter(Files::isDirectory).forEach(CqCommonUtils::deleteDirectory);
/*
* Unpack the community jars of excluded components to their target/classes so that Camel plugins find it there
*/
project.getBasedir();
excludes.stream().map(ga -> fullTree.getModulesByGa().get(ga)).filter(CamelProdExcludesMojo::isComponent).forEach(module -> {
final String artifactId = module.getGav().getArtifactId().asConstant();
final Path jarPath = CqCommonUtils.resolveArtifact(Paths.get(localRepository), "org.apache.camel", artifactId, camelCommunityVersion, "jar", repositories, repoSystem, repoSession);
final Path pomFilePath = basePath.resolve(module.getPomPath());
final Path moduleBaseDir = pomFilePath.getParent();
final File outputDir = moduleBaseDir.resolve("target/classes").toFile();
try (ZipFile zipFile = new ZipFile(jarPath.toFile())) {
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();
final File entryDestination = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
try (InputStream in = zipFile.getInputStream(entry);
OutputStream out = new FileOutputStream(entryDestination)) {
IOUtils.copy(in, out);
}
}
}
} catch (IOException e) {
throw new RuntimeException("Could not extract " + jarPath + " to " + outputDir);
}
/* Execute ComponentDslMojo in the excluded component modules */
getLog().info("Executing ComponentDslMojo in " + moduleBaseDir);
final File origFile = project.getFile();
project.setFile(pomFilePath.toFile());
final String originalBuildDir = project.getBuild().getDirectory();
project.getBuild().setDirectory(moduleBaseDir.resolve("target").toString());
try {
ComponentDslMojo mojo = new ComponentDslMojo();
mojo.setLog(getLog());
mojo.setPluginContext(getPluginContext());
mojo.execute(project, projectHelper, new DefaultBuildContext());
} catch (Exception e) {
throw new RuntimeException("Could not excute ComponentDslMojo in " + moduleBaseDir, e);
} finally {
project.setFile(origFile);
project.getBuild().setDirectory(originalBuildDir);
}
});
}
Aggregations