简体   繁体   English

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

[英]cannot set classpath with maven-assembly-plugin

I am creating console application. 我正在创建控制台应用程序 I want to have configuration files outside the jar file in conf folder and want to register this folder as a classpath for my application. 我想在conf文件夹中的jar文件外部conf文件,并希望将此文件夹注册为我的应用程序的类路径。

I run mvn assembly:single command , 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:single命令,获取一个jar文件,但是当我尝试用java -jar MyApplication.jar运行这个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>

It was my mistake, I had to put 这是我的错,我不得不放

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

and not 并不是

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

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 看待

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM