我最终在 NetBeans 7.3.1 中为我的类似案例使用了一个开箱即用的解决方案:
Adding files to java classpath at runtime
private static void addSoftwareLibrary(File file) throws Exception {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
method.invoke(ClassLoader.getSystemClassLoader(), new Object[]{file.toURI().toURL()});
}
这为我提供了一种在运行时通过程序参数将文件添加到我的类路径的方法。研究时整理的相关笔记:
要包含仅用于编译的依赖项,not runtime 设置:
Dependency Scope
<dependency><scope>provided</scope>...</dependency>
要从阴影 jar 中排除依赖项,请设置:Exclude
<exclude>groupId:artifactId[[:type]:classifier]</exclude>
将资源从典型源目录之外复制到目标目录:Copy Resources
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>extra-resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
请注意,目录的基本路径是项目主目录。链接的帖子有 <filtering>true</filtering>,这可能会导致 Netbeans 7.3.1 中出现“无效标记”。