[英]Maven appassembler plugin: change current directory before launching program
假设我有一个带有Main.java
的简单Spring应用程序,如下所示:
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)
查看appassembler
生成的foo
脚本后,我注意到它定义了$BASEDIR
,它实际上是包含已汇编的应用程序的目录(在我的情况下$HOME/work/java/foo/target/appassembler
)。
尽管$BASEDIR
传递给程序调用,但似乎似乎错过了一步,因为它不首先对cd $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
属性,但这只是一连串的麻烦...)
好的,以防将来对其他任何人有用,我能够更改用于生成脚本的模板以根据需要更改目录。
原始模板可从此处获取 。 放入您的项目并根据需要进行编辑。
然后只需将以下内容添加到appassembler配置部分:
<configuration>
...
<unixScriptTemplate>${project.basedir}/path/to/script/template</unixScriptTemplate>
</configuration>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.