假设我有一个简单的 Spring 应用程序,Main.java
如下所示:
public class Main {
public static void main(String[] args) {
AbstractApplicationContext context = new
FileSystemXmlApplicationContext("config/application-context.xml");
// do stuff...
}
}
我正在使用 Maven 构建项目并使用该appassembler
插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.8</version>
<configuration>
<programs>
<program>
<mainClass>com.jonarcher.Main</mainClass>
<id>foo</id>
</program>
</programs>
<target>${project.build.directory}</target>
<repositoryLayout>flat</repositoryLayout>
<repositoryName>lib</repositoryName>
<configurationDirectory>config</configurationDirectory>
<configurationSourceDirectory>src/main/config</configurationSourceDirectory>
<copyConfigurationDirectory>true</copyConfigurationDirectory>
</configuration>
</plugin>
我有我的项目,$HOME/work/java/foo
所以我:
$ cd $HOME/work/java/foo
$ mvn clean package
... usual copious maven output ...
# make the script executable
$ chmod 755 target/appassembler/bin/foo
# run it
$ target/appassembler/bin/foo
# aw...FileNotFoundException!
16:42:21,329 INFO .support.FileSystemXmlApplicationContext: 515 - Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@11831d18: startup date [Wed Dec 17 16:42:21 MST 2014]; root of context hierarchy
16:42:21,369 INFO eans.factory.xml.XmlBeanDefinitionReader: 316 - Loading XML bean definitions from file [/Users/jarcher/work/java/foo/config/application-context.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/Users/jarcher/work/java/foo/config/application-context.xml]; nested exception is java.io.FileNotFoundException: config/steve.xml (No such file or directory)
查看生成的foo
脚本,appassembler
我注意到它定义了一个$BASEDIR
实际上是包含组装应用程序的目录(即$HOME/work/java/foo/target/appassembler
在我的情况下)。
虽然$BASEDIR
被传递给程序调用,但它似乎是一个错过的步骤,因为它不是cd $BASEDIR
首先......
exec "$JAVACMD" $JAVA_OPTS \
-classpath "$CLASSPATH" \
-Dapp.name="steve" \
-Dapp.pid="$$" \
-Dapp.repo="$REPO" \
-Dapp.home="$BASEDIR" \
-Dbasedir="$BASEDIR" \
com.jonarcher.Main \
"$@"
好的,所以我可以:
$ cd $HOME/work/java/foo/target/appassembler # or wherever my app ultimately is installed
$ bin/foo
一切都很好,但我真的不希望人们必须在正确的目录中才能启动它(!)
所以我错过了什么吗?有没有办法轻松改变这种情况?我想错了吗?
(我意识到我可以使用app.home
传入的属性,但这只是一连串的麻烦......)