79724847

Date: 2025-08-04 11:30:02
Score: 1.5
Natty:
Report link

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.

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Nathalie Kitty