java 无法使用 maven-assembly-plugin 设置类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15685196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:30:13  来源:igfitidea点击:

cannot set classpath with maven-assembly-plugin

javamaven-2classpathpom.xmlexecutable-jar

提问by D.R.

I am creating console application. I want to have configuration files outside the jarfile in conffolder and want to register this folder as a classpath for my application.

我正在创建控制台应用程序。我希望在jar文件conf夹中的文件之外拥有配置文件,并希望将此文件夹注册为我的应用程序的类路径。

I run mvn assembly:singlecommand , get a jar file, BUT when I try to run this JAR with java -jar MyApplication.jar, it can't read configuration files.

我运行mvn assembly:singlecommand ,获取一个 jar 文件,但是当我尝试使用 运行这个 JAR 时java -jar MyApplication.jar,它无法读取配置文件。

I have this snippet in my pom.xml

我有这个片段在我的 pom.xml

<build>
    <finalName>MyApplication</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.7</version>  
            <configuration>
                <projectNameTemplate>
                    [artifactId]-[version]
                </projectNameTemplate>
                <wtpmanifest>true</wtpmanifest>
                <wtpapplicationxml>true</wtpapplicationxml>
                <wtpversion>2.0</wtpversion>
                <manifest>
                    ${basedir}/src/main/resources/META-INF/MANIFEST.MF
                </manifest>
            </configuration>

        </plugin>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <appendAssemblyId>false</appendAssemblyId>
                <archive>
                    <manifest>
                        <mainClass>com.my.test.App</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>.conf/</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

采纳答案by D.R.

It was my mistake, I had to put

这是我的错误,我不得不把

<Class-Path>./conf/</Class-Path>

and not

并不是

<Class-Path>.conf/</Class-Path>

回答by Marc

I usually do not use the assembly plugin to generate classpath entry in MANIFEST but rather the maven-jar-plugin with this configuration :

我通常不使用程序集插件在 MANIFEST 中生成类路径条目,而是使用具有以下配置的 maven-jar-plugin :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
      <archive>
        <index>true</index>
        <manifest>
          <addClasspath>true</addClasspath>
          <addExtensions>false</addExtensions>
          <mainClass>com.my.test.App</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>

I only use the assembly plugin to copy dependencies (including transitive ones) into my build directory, and creating the distribution archive. You can also use the dependency plugin do do this. If you want to copy you dependencies into a sub directory of your distribution tree, use the classpathPrefix in the maven-jar-plugin configuration to match you assembly descriptor dependencies destination.

我只使用程序集插件将依赖项(包括可传递的)复制到我的构建目录中,并创建分发存档。你也可以使用依赖插件来做到这一点。如果要将依赖项复制到分发树的子目录中,请使用 maven-jar-plugin 配置中的 classpathPrefix 来匹配程序集描述符依赖项目标。

Regard

看待