Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding a Maven dependency from a property

Tags:

maven

I'm trying to add a system scoped dependency to my POM. The trick is that I do not have the systemPath. Instead, I have a path to a properties file that contains a property that I can use as the path.

I've tried to use the properties-maven-plugin to deal with this, but it seems that the system dependency gets resolved before the plugin runs, and so the property that I'm trying to define is not available soon enough.

Here's the section of my POM:

<profiles>
    <profile>
        <id>dylink</id>
        <dependencies>
            <dependency>
                <artifactId>outside-lib</artifactId>
                <groupId>com.foo</groupId>
                <version>1.0</version>
                <scope>system</scope>
                <systemPath>${outsidelib.location}/outside-lib.jar</systemPath>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>properties-maven-plugin</artifactId>
                    <version>1.0-alpha-2</version>
                    <executions>
                        <execution>
                            <phase>initialize</phase>
                            <goals>
                                <goal>read-project-properties</goal>
                            </goals>
                            <configuration>
                                <files>
                                    <file>C:\path\to\file.properties</file>
                                </files>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

And I run something like mvn -P dylink compile and get told that my systemPath is invalid because it's not an absolute path. (The property contains an absolute path)

Alternatively, is there any other way I can do this? I need to be able to query the host system for the location of the system-scope dependency. It won't work to just hard-code the path into the POM.

like image 549
Nathaniel Waisbrot Avatar asked May 20 '26 03:05

Nathaniel Waisbrot


1 Answers

It is not possible. In order to execute plugins, maven needs to have everything required 'downloaded' in the local repository. It needs to be sure of the dependencies' versions, which are often defined in properties. Hence, it needs to start by finding the property values before downloading all that is missing, before executing plugins. You want the plugins to be executed before property values are defined...

I guess the properties plugins adds extra property values when executed, but that happens after the maven dependency check...

like image 164
Jérôme Verstrynge Avatar answered May 22 '26 18:05

Jérôme Verstrynge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!