How to collect all files of a server in a pom.xml to have them in the IDE

Knowledge base / Microblog about software development related things by Hans-Peter Störr

How to collect all files of a server in a pom.xml to have them in the IDE

Created 28-03-2024, last change 28-03-2024

Sometimes you want all JARs that are in a server to be available in your IDE - e.g. if you want to test or debugsome code that uses deployed code but like to run it outside of the server itself. Here is a script that creates a pom.xml with all JARs of a server in it. You might need to adjust it to your needs. To be run in the top directory of the server.

import java.util.zip.ZipEntry
import java.util.zip.ZipFile

File topfile = new File("sling").canonicalFile
if (!topfile.exists()) {
    topfile = new File("launcher").canonicalFile
}
if (!topfile.exists()) {
    topfile = new File("crx-quickstart").canonicalFile
}

def pom = new FileWriter("pom.xml")
pom.append("""<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>hps.launchpaddeps</groupId>
    <artifactId>launchpadaem</artifactId>
    <version>0.0.0-SNAPSHOT</version>
    
    <!-- Generated by GenerateFelixPom.groovy. -->
    
    <properties>
        <source.encoding>UTF-8</source.encoding>
        <java.source>1.8</java.source>
        <java.target>1.8</java.target>
    </properties>

    <dependencies>
""")

List<String> entries = new ArrayList<String>();

topfile.traverse(nameFilter: ~/.*\.jar/) { File jarfile ->
    def jar = new ZipFile(jarfile)
    String pomentry = ""
    jar.entries().find { ZipEntry entry -> entry.name.endsWith("/pom.properties") && entry.name.contains("META-INF/maven") }.each {
        Properties props = new Properties()
        props.load(jar.getInputStream(it))
        def version = props.getProperty("version")
        def group = props.getProperty("groupId")
        def artifact = props.getProperty("artifactId")
        String absolutepath = jarfile.canonicalPath
        pomentry = pomentry + """
        <dependency>
            <groupId>$group</groupId>
            <artifactId>$artifact</artifactId>
            <version>$version</version>
            <scope>system</scope>
            <systemPath>$absolutepath</systemPath>
        </dependency>
"""
    }
    if (pomentry == "") {
        def artifact = jarfile.name.replaceAll(~/[^a-zA-Z0-9]/, "_")
        String relativepath = jarfile.canonicalPath.substring(topfile.parentFile.path.length())
        String absolutepath = jarfile.canonicalPath
        pomentry = """
        <dependency>
            <groupId>hps.launchpaddeps</groupId>
            <artifactId>$artifact</artifactId>
            <version>0.0.0-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>$absolutepath</systemPath>
        </dependency>
"""
    }
    // println(pomentry)
    entries.add(pomentry)
    pomentry = ""
    jar.close()
}

Collections.sort(entries)
for(item in entries){
    pom.append(item)
}

pom.append("""
    </dependencies>
</project>
""")
pom.close()

println("Wrote pom.xml")