Setting custom runtime classpath for a maven proje

2019-02-28 21:25发布

问题:

I want to add a custom classpath when I'm running my maven project from within netbeans. So far I've tried adding the following to the Run Project action in the project properties:

exec.args=-classpath %classpath;c:/QUASR/duplicateRemoval.jar;c:/QUASR/lib/QUASR.jar ${packageClassName} 

exec.args=-cp %classpath;c:/QUASR/duplicateRemoval.jar;c:/QUASR/lib/QUASR.jar ${packageClassName}

exec.args=-cp c:/QUASR/duplicateRemoval.jar;c:/QUASR/lib/QUASR.jar ${packageClassName}  

but no luck, the custom runtime classpath is not set.

回答1:

You should add a new profile run-with-netbeans in your pom that declares the additional dependencies (use the provided scope to not include them in the release).

Then you'll have to add the new profile to your IDE to run the pom with the -P run-with-netbeans option in the command line.

<properties>
    <!-- provided by default -->
    <my-dynamic-scope>provided</my-dynamic-scope>
</properties>

<profiles>
    <profile>
        <id>run-with-netbeans</id>
        <properties>
            <!-- compile when running in IDE -->
            <my-dynamic-scope>compile</my-dynamic-scope>
        </properties>
        <dependencies>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
        </dependencies>
    </profile>
</profiles>


<dependencies>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>${commons-lang.version}</version>
        <scope>${my-dynamic-scope}</scope>
    </dependency>
</dependencies>

The snippet above add log4j only when running with the run-with-netbeans profile. It also sets a property my-dynamic-scope that can be used in your dependency block to change the scope.

HIH M.