javamaven

How to list dependencies into a file during 'mvn clean install'


I have plugin.yml file in resources folder with placeholders like ${project.name}. In pom.xml I have following section

<resource>
  <directory>src/main/resources</directory>
  <filtering>true</filtering>
</resource>

which allows Maven to replace these placeholders.
How can I list dependencies into the same file, so that each dependency like

<dependency>
  <groupId>javax.annotation</groupId>
  <artifactId>javax.annotation-api</artifactId>
  <version>1.3.2</version>
  <scope>provided</scope>
</dependency>

turns into an element in a list: libraries: [javax.annotation:javax.annotation-api:1.3.2, other:dependency:1.0]?
How can I also filter out specific dependencies from being included in this list?


Solution

  • Thanks to answers by SpaceTrucker and Gerold Broser I managed to make this work!
    Using maven-dependency-plugin:collect I put all the libraries into a text file, then in process-resources phase a script formats the file contents how I want and injects them into its destination file in target folder.

    plugins in pom.xml:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.8.1</version>
        <executions>
            <execution>
                <id>collect</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>collect</goal>
                </goals>
                <configuration>
                    <outputFile>scripts/libraries.txt</outputFile>
                    <outputScope>false</outputScope>
                    <outputEncoding>UTF-8</outputEncoding>
                    <excludeArtifactIds>purpur-api, paper-api, spigot-api</excludeArtifactIds>
                    <excludeTransitive>true</excludeTransitive>
                    <excludeScope>runtime</excludeScope><!-- excludes runtime+compile -->
                    <silent>true</silent>
                    <sort>true</sort>
                </configuration>
            </execution>
        </executions>
    </plugin><!-- maven-dependency-plugin @ generate-sources -->
    <plugin>
        <artifactId>exec-maven-plugin</artifactId>
        <groupId>org.codehaus.mojo</groupId>
        <version>3.5.1</version>
        <executions>
            <execution>
                <id>exec-script</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>scripts/injectLibrariesIntoResources-v2.6.bat</executable>
                </configuration>
            </execution>
        </executions>
    </plugin><!-- exec-maven-plugin @ process-resources -->
    

    script in scripts/injectLibrariesIntoResources-v2.6.bat:

    @echo off
    cd scripts
    
    set librariesFile="libraries.txt"
    set targetFile="../target/classes/plugin.yml"
    
    powershell -command "(Get-Content '%librariesFile%') | ? {$_.trim() -ne ''} | Set-Content '%librariesFile%'"
    powershell -command "(Get-Content '%librariesFile%') -replace 'The following files have been resolved:', '' | Set-Content '%librariesFile%'"
    powershell -command "(Get-Content '%librariesFile%') -replace ':jar|.*$', '' | Set-Content '%librariesFile%'"
    powershell -command "(Get-Content '%librariesFile%') -replace '   ', '- ' | Set-Content '%librariesFile%'"
    
    powershell -command "(Get-Content '%targetFile%' -Raw) -replace ' \[\] #libraries', (Get-Content '%librariesFile%' -Raw) | Set-Content '%targetFile%'"
    

    which changes line in plugin.yml from

    libraries: [] #libraries
    

    to

    libraries:
    - com.google.code.gson:gson:2.13.1
    - javax.annotation:javax.annotation-api:1.3.2
    - org.apache.commons:commons-lang3:3.18.0
    

    Why is the script in bash but uses powershell? I wanted this setup to be easily portable! Executing powershell script files requires changing execution policy in Windows.