我为模块添加了资源,如下所示:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>nbm-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<moduleType>eager</moduleType>
<nbmResources>
<nbmResource>
<directory>${basedir}/resources</directory>
<targetPath>resources</targetPath>
<includes>
<include>*.db</include>
</includes>
<excludes>
<exclude>*trace*</exclude>
</excludes>
</nbmResource>
</nbmResources>
</configuration>
</plugin>
该文件显示在 Netbeans 应用程序中:
target/app name/app name/resources/card_manager.mv.db
。看起来不错。
现在我尝试按如下方式获取该文件位置:
File db = InstalledFileLocator.getDefault().locate("card_manager.mv.db",
"module.codename.base", false);
但是 db 始终为空。有什么想法吗?
试试这个代码...
File file = InstalledFileLocator.getDefault().locate("myfile", null, false);
if (file == null)
{
file = new File(Places.getUserDirectory() + File. separator + "myfile");
}
或者您尝试过 resources/card_manager.mv.db 吗?我确定应用程序路径(应用程序名称)和集群名称(第二个应用程序名称)被排除在locate()之外,但我相信您必须包含资源/路径。
修改您的 pom,以便将
card_manager.mv.db
复制到类文件夹而不是资源文件夹,并从类加载器中获取文件。
我也有同样的问题。在 Maven 中,模块代号基础包括 pom groupId。构建应用程序后,您可以通过检查
application/target/appname/appname/modules
中的 .jar 文件名来检查确切的代码名库(请注意,.jar 文件使用“-”而不是“.”)。
例如,在我使用的基于 Ant 的应用程序中:
File f = InstalledFileLocator.getDefault().locate("modules/soundfont/JJazzLab-SoundFont.sf2, "org.jjazz.fluidsynthembeddedsynth", false);
切换到 Maven 时,我必须为模块 groupId 添加前缀“org.jjazzlab”:
File f = InstalledFileLocator.getDefault().locate("modules/soundfont/JJazzLab-SoundFont.sf2, "org.jjazzlab.org.jjazz.fluidsynthembeddedsynth", false);
所以对你来说应该是:
File db = InstalledFileLocator.getDefault().locate("resources/card_manager.mv.db", "groupId.module.codename.base", false);
如果您需要在 Maven 模块中工作的完整示例,请参阅
https://github.com/jjazzboss/JJazzLab/blob/dev/app/FluidSynthEmbeddedSynth/src/main/java/org 中
SOUNDFONT_FILE
的加载/jjazz/fluidsynthembeddedsynth/FluidSynthEmbeddedSynth.java