Maven appassembler插件:在启动程序之前更改当前目录

时间:2014-12-18 01:13:45

标签: java spring maven appassembler

假设我有一个简单的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属性,但这只是一个麻烦的级联链......)

1 个答案:

答案 0 :(得分:2)

如果以后这对其他任何人都有用,我可以改变用于生成脚本的模板来更改目录。

可以从here隐藏原始模板。放入项目并根据需要进行编辑。

然后只需将以下内容添加到appassembler配置部分:

<configuration>
    ...
    <unixScriptTemplate>${project.basedir}/path/to/script/template</unixScriptTemplate>
</configuration>