Search in sources :

Example 1 with DependencyInfo

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;
}
Also used : PluginExecution(org.apache.maven.model.PluginExecution) Repository(org.apache.maven.model.Repository) Build(org.apache.maven.model.Build) Model(org.apache.maven.model.Model) Dependency(org.apache.maven.model.Dependency) DependencyInfo(io.jenkins.tools.warpackager.lib.config.DependencyInfo) Plugin(org.apache.maven.model.Plugin)

Example 2 with DependencyInfo

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);
}
Also used : Path(java.nio.file.Path) Transformer(javax.xml.transform.Transformer) DOMSource(javax.xml.transform.dom.DOMSource) Arrays(java.util.Arrays) TransformerException(javax.xml.transform.TransformerException) Enumeration(java.util.Enumeration) StreamResult(javax.xml.transform.stream.StreamResult) StringUtils(org.apache.commons.lang3.StringUtils) Level(java.util.logging.Level) HashSet(java.util.HashSet) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) Map(java.util.Map) ZipFile(java.util.zip.ZipFile) Nonnull(javax.annotation.Nonnull) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) NodeList(org.w3c.dom.NodeList) Files(java.nio.file.Files) DependencyInfo(io.jenkins.tools.warpackager.lib.config.DependencyInfo) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) GroovyHookInfo(io.jenkins.tools.warpackager.lib.config.GroovyHookInfo) Element(org.w3c.dom.Element) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Config(io.jenkins.tools.warpackager.lib.config.Config) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) MavenHelper(io.jenkins.tools.warpackager.lib.util.MavenHelper) CheckForNull(javax.annotation.CheckForNull) InputStream(java.io.InputStream) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 3 with DependencyInfo

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");
    }
}
Also used : Path(java.nio.file.Path) Transformer(javax.xml.transform.Transformer) DOMSource(javax.xml.transform.dom.DOMSource) Arrays(java.util.Arrays) TransformerException(javax.xml.transform.TransformerException) Enumeration(java.util.Enumeration) StreamResult(javax.xml.transform.stream.StreamResult) StringUtils(org.apache.commons.lang3.StringUtils) Level(java.util.logging.Level) HashSet(java.util.HashSet) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) Map(java.util.Map) ZipFile(java.util.zip.ZipFile) Nonnull(javax.annotation.Nonnull) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) NodeList(org.w3c.dom.NodeList) Files(java.nio.file.Files) DependencyInfo(io.jenkins.tools.warpackager.lib.config.DependencyInfo) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) GroovyHookInfo(io.jenkins.tools.warpackager.lib.config.GroovyHookInfo) Element(org.w3c.dom.Element) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Config(io.jenkins.tools.warpackager.lib.config.Config) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) MavenHelper(io.jenkins.tools.warpackager.lib.util.MavenHelper) CheckForNull(javax.annotation.CheckForNull) InputStream(java.io.InputStream) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 4 with DependencyInfo

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");
}
Also used : HashMap(java.util.HashMap) SimpleManifest(io.jenkins.tools.warpackager.lib.util.SimpleManifest) GroovyHookInfo(io.jenkins.tools.warpackager.lib.config.GroovyHookInfo) Model(org.apache.maven.model.Model) File(java.io.File) DependencyInfo(io.jenkins.tools.warpackager.lib.config.DependencyInfo)

Aggregations

DependencyInfo (io.jenkins.tools.warpackager.lib.config.DependencyInfo)4 GroovyHookInfo (io.jenkins.tools.warpackager.lib.config.GroovyHookInfo)3 File (java.io.File)3 Config (io.jenkins.tools.warpackager.lib.config.Config)2 MavenHelper (io.jenkins.tools.warpackager.lib.util.MavenHelper)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Arrays (java.util.Arrays)2 Enumeration (java.util.Enumeration)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Level (java.util.logging.Level)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 ZipEntry (java.util.zip.ZipEntry)2