【问题标题】:Maven: Not copying files from src/main/resources to the classpath root - Executable JarMaven:不将文件从 src/main/resources 复制到类路径根目录 - 可执行 Jar
【发布时间】:2013-09-06 13:07:00
【问题描述】:

我正在使用 Assembly 插件使用 Maven 构建一个可执行 jar。我有一个位于 src/main/resources 的资源文件(.xml)。当我构建可执行 jar 时,文件没有被复制到 jar 中 - 通过解压 jar 进行检查。

这是我的 pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>package-jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>xx.com.xxx.xxxx.xx.xxxx.InterfaceRunner</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>.</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </execution>
    </executions>
</plugin>

我正在尝试调用保存在 src/main/resources 下的以下资源:

reader = Resources.getResourceAsReader("mybatis-configuration.xml");

执行java -jar InterfaceRunner.jar时出现以下异常

Exception caught while reading or parsing the mybatis config xml :java.io.IOException: Could not find resource mybatis-configuration.xml
    at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:108)
    at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:95)
    at org.apache.ibatis.io.Resources.getResourceAsReader(Resources.java:153)

以前有没有人遇到过类似的问题?寻求您的帮助,Maven 大师..

【问题讨论】:

  • mvn clean package 检查构建 jar 是否有资源?

标签: java eclipse maven jar executable


【解决方案1】:

您可以尝试如下替换您的Configuration

<configuration>
        <archive>
                <manifest>
                        <mainClass>xx.com.xxx.xxxx.xx.xxxx.InterfaceRunner</mainClass>
                </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    <finalName>InterfaceRunner</finalName>
</configuration>

然后

mvn package 

【讨论】:

  • 不确定这是否是问题的原因:.
  • 我需要将 Classpath 设置为 root,以便从外部访问一些属性文件
【解决方案2】:

默认情况下,maven-assembly-plugin 制作两个罐子,而不是一个(带有阶段包)。确保您正在运行名为&lt;artifactid&gt;-&lt;version&gt;-jar-with-dependencies.jar

如果这不是问题,你是不是在你的 pom 中做了什么奇怪的事情:

<build>
    <resources>
        <resource>
             <targetPath><!-- here?? --></targetPath>
        </resource>
    </resources>
    <!-- etc. -->
</build>

【讨论】:

  • 我正在使用 Spring Tool Suite(STS) 使用 Maven 进行构建,并且正在使用 maven install 命令。尽管在目标位置创建了一个名为 - -.jar 的 jar
  • 我也没有在 标签内设置任何东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 2016-10-12
相关资源
最近更新 更多