use of io.jenkins.tools.warpackager.lib.config.DependencyInfo in project custom-war-packager by jenkinsci.
the class MavenHPICustomWARPOMGenerator method generatePOM.
public Model generatePOM(Map<String, String> versionOverrides) throws IOException {
Model model = new Model();
model.setModelVersion("4.0.0");
model.setGroupId(config.bundle.groupId);
model.setArtifactId(config.bundle.artifactId);
if (config.bundle.description != null) {
model.setDescription(config.bundle.description);
}
model.setVersion(config.buildSettings.getVersion());
// WAR Dependency
Dependency dep = config.war.toDependency(versionOverrides);
dep.setScope("test");
dep.setType("war");
model.addDependency(dep);
// Plugins
if (config.plugins != null) {
for (DependencyInfo plugin : config.plugins) {
Dependency pluginDep = plugin.toDependency(versionOverrides);
pluginDep.setScope("runtime");
model.addDependency(pluginDep);
}
}
// Maven HPI Plugin
/* Sample:
<plugin>
<groupId>≈/groupId>
<artifactId>maven-hpi-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>package-war</id>
<goals>
<goal>custom-war</goal>
</goals>
<configuration>
<outputFile>${build.directory}/tools-modified.war</outputFile>
</configuration>
</execution>
</executions>
</plugin>
*/
Repository jenkinsRepository = new Repository();
jenkinsRepository.setId("repo.jenkins-ci.org");
jenkinsRepository.setUrl("https://repo.jenkins-ci.org/public/");
model.addPluginRepository(jenkinsRepository);
model.addRepository(jenkinsRepository);
Plugin mavenHPIPlugin = new Plugin();
mavenHPIPlugin.setGroupId("org.jenkins-ci.tools");
mavenHPIPlugin.setArtifactId("maven-hpi-plugin");
// TODO: make configurable
mavenHPIPlugin.setVersion("2.2");
PluginExecution execution = new PluginExecution();
execution.setId("package-war");
execution.addGoal("custom-war");
execution.setConfiguration(generateCustomWarGoalConfiguration());
mavenHPIPlugin.addExecution(execution);
Build build = new Build();
build.addPlugin(mavenHPIPlugin);
model.setBuild(build);
return model;
}
use of io.jenkins.tools.warpackager.lib.config.DependencyInfo in project custom-war-packager by jenkinsci.
the class JenkinsWarPatcher method replaceLib.
private void replaceLib(DependencyInfo lib, Map<String, String> versionOverrides) throws IOException, InterruptedException {
if (lib.source == null) {
throw new IOException("Source is not defined for " + lib);
}
File libsDir = getLibsDir();
String effectiveVersion = versionOverrides.get(lib.artifactId);
if (effectiveVersion == null) {
if (!lib.source.isReleasedVersion()) {
throw new IOException("Cannot resolve new version for library " + lib);
}
effectiveVersion = lib.source.version;
}
List<Path> paths = Files.find(libsDir.toPath(), 1, (path, basicFileAttributes) -> {
// TODO: this matcher is a bit lame, it may suffer from false positives
String fileName = String.valueOf(path.getFileName());
if (fileName.matches(lib.artifactId + "-\\d+.*")) {
return true;
}
return false;
}).collect(Collectors.toList());
if (paths.size() > 1) {
throw new IOException("Bug in Jenkins WAR Packager, cannot find unique lib JAR for artifact " + lib.artifactId + ". Candidates: " + StringUtils.join(paths, ","));
} else if (paths.size() == 1) {
Path oldFile = paths.get(0);
LOGGER.log(Level.INFO, "Replacing the existing library {0} by version {1}. Original File: {2}", new Object[] { lib.artifactId, effectiveVersion, oldFile.getFileName() });
Files.delete(oldFile);
} else {
LOGGER.log(Level.INFO, "Adding new library {0} with version {1}", new Object[] { lib.artifactId, effectiveVersion });
}
File newJarFile = new File(libsDir, lib.artifactId + "-" + effectiveVersion + ".jar");
MavenHelper.downloadArtifact(dstDir, lib, effectiveVersion, newJarFile);
}
use of io.jenkins.tools.warpackager.lib.config.DependencyInfo in project custom-war-packager by jenkinsci.
the class JenkinsWarPatcher method excludeLib.
private void excludeLib(DependencyInfo lib) throws IOException, InterruptedException {
File libsDir = getLibsDir();
List<Path> paths = Files.find(libsDir.toPath(), 1, (path, basicFileAttributes) -> {
// TODO: this matcher is a bit lame, it may suffer from false positives
String fileName = String.valueOf(path.getFileName());
if (fileName.matches(lib.artifactId + "-\\d+.*")) {
return true;
}
return false;
}).collect(Collectors.toList());
if (paths.size() > 1) {
throw new IOException("Bug in Jenkins WAR Packager, cannot find unique lib JAR for artifact " + lib.artifactId + ". Candidates: " + StringUtils.join(paths, ","));
} else if (paths.size() == 1) {
Path oldFile = paths.get(0);
LOGGER.log(Level.INFO, "Removing library {0}. Original File: {1}", new Object[] { lib.artifactId, oldFile.getFileName() });
Files.delete(oldFile);
} else {
throw new IOException("Cannot remove library " + lib + ". It is missing in the WAR file");
}
}
use of io.jenkins.tools.warpackager.lib.config.DependencyInfo in project custom-war-packager by jenkinsci.
the class Builder method build.
public void build() throws IOException, InterruptedException {
// Cleanup the temporary directory
final File tmpDir = config.buildSettings.getTmpDir();
if (tmpDir.exists()) {
LOGGER.log(Level.INFO, "Cleaning up the temporary directory {0}", tmpDir);
FileUtils.deleteDirectory(tmpDir);
}
Files.createDirectories(buildRoot.toPath());
// Build core and plugins
buildIfNeeded(config.war, "war");
if (config.plugins != null) {
for (DependencyInfo plugin : config.plugins) {
buildIfNeeded(plugin, "hpi");
}
}
// Prepare library patches
if (config.libPatches != null) {
for (DependencyInfo library : config.libPatches) {
buildIfNeeded(library, "jar");
}
}
// Prepare Groovy Hooks
Map<String, File> hooks = new HashMap<>();
if (config.groovyHooks != null) {
for (GroovyHookInfo hook : config.groovyHooks) {
hooks.put(hook.id, checkoutIfNeeded(hook.id, hook.source));
}
}
// Generate POM
File warBuildDir = new File(tmpDir, "prebuild");
Files.createDirectories(warBuildDir.toPath());
MavenHPICustomWARPOMGenerator gen = new MavenHPICustomWARPOMGenerator(config, "-prebuild");
Model model = gen.generatePOM(versionOverrides);
gen.writePOM(model, warBuildDir);
// Build WAR using Maven HPI plugin
processFor(warBuildDir, "mvn", "clean", "package");
// Add System properties
File srcWar = new File(warBuildDir, "target/" + config.bundle.artifactId + "-prebuild.war");
File explodedWar = new File(warBuildDir, "exploded-war");
// Patch WAR
new JenkinsWarPatcher(config, srcWar, explodedWar).removeMetaInf().addSystemProperties(config.systemProperties).replaceLibs(versionOverrides).excludeLibs().addHooks(hooks);
File warOutputDir = new File(tmpDir, "output");
SimpleManifest manifest = SimpleManifest.parseFile(srcWar);
MavenWARPackagePOMGenerator finalWar = new MavenWARPackagePOMGenerator(config, explodedWar);
finalWar.writePOM(finalWar.generatePOM(manifest.getMain()), warOutputDir);
processFor(warOutputDir, "mvn", "clean", "package");
// TODO: Support custom output destinations
// File dstWar = new File(warBuildDir, "target/" + config.bundle.artifactId + ".war");
}
Aggregations