我有一个Java项目,它使用.properties文件进行配置。在服务器上,启动时,我将类路径设置为包括一个包含所有属性文件的文件夹。在我的本地计算机上,我想指向另一个文件夹。
我希望将其添加到类路径中,最适合所有项目,但也可以将其添加到每个项目中。我尝试过更改Run > VM Options
以包括类路径,但是通过这种更改,它找不到主类,而我得到了java.lang.NoClassDefFoundError
。我还尝试过直接更改nbactions.xml来将类路径设置为-classpath ~\MyFolder\;%classpath
,但这存在相同的问题。
更麻烦的是,服务器运行的是linux,而我的本地计算机的是Windows。
最佳答案
我也长期关注主题启动器问题。我的目标-将用于调试目的的配置文件放在项目根目录中,并将类路径扩展到${basedir}
,因此此代码:
String appConfigLocation = System.getProperty("config.location");
if (appConfigLocation == null) {
logger.error("System property 'config.location' is not set...");
System.exit(1);
}
InputStream appConfigStream = Main.class.getClassLoader().getResourceAsStream(appConfigLocation);
if (appConfigStream == null) {
logger.error("Can't find resource {} in classpath, fix 'config.location'...", appConfigLocation);
System.exit(1);
}
Properties appProps = new Properties();
try {
appProps.load(appConfigStream);
} catch (IOException ex) {
System.out.println("IO error during loading of {}...", appConfigLocation);
System.exit(1);
}
从
${basedir}
读取配置。我喜欢这样,而不是将它们放在src/main/resources
上。检查
ExecMojo.java
的v1.2.1 http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.mojo/exec-maven-plugin/1.2.1/org/codehaus/mojo/exec/ExecMojo.java?av=f的来源:if ( CLASSPATH_TOKEN.equals( args[i] ) ) {
commandArguments.add( computeClasspathString( null ) );
}
和v1.3.2 http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.mojo/exec-maven-plugin/1.3.2/org/codehaus/mojo/exec/ExecMojo.java?av=f:
if ( args[i].contains( CLASSPATH_TOKEN ) ) {
commandArguments.add( args[i].replace( CLASSPATH_TOKEN,
computeClasspathString( null ) ) );
}
因此,将NB config 更新为新版本,执行目标:
process-classes org.codehaus.mojo:exec-maven-plugin:1.3.2:exec
并在
-classpath
参数中使用复杂的exec.args
参数:exec.args=-classpath %classpath:.:"${basedir}" \
-Dconfig.location=app.properties \
-Dlogback.configurationFile=logback.xml \
${packageClassName}
解决任何具有这种行为所需的操作:
也可以看看: