0

我无法创建一个包含所有依赖项和资源的可执行 jar。到目前为止,我在 jar 中拥有所有东西,但它正在寻找 jar 外部的资源。

这是我的项目的结构:

MyProject
----images
----resources
----src
----...

我能够创建包含依赖项的可执行 Jar,并且资源也被打包,但是它仍在“资源”文件夹中而不是 JAR 中查找资源。

罐子看起来像这样:

MyProject.jar
----images
----resources
----...

当我尝试运行它时,我收到此错误:

java.io.FileNotFoundException: .../MyProject/target/resources/default-style.xml (No such file or directory)

所以我的问题是这样的:我的依赖项和资源像我想要的那样打包在 jar 中,但是在执行时它在“target/”文件夹中而不是在 JAR 中寻找“resources/”。

这是我的pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- MyProject INFO -->
<groupId>Main</groupId>
<artifactId>MyProject</artifactId>
<version>0.0.1-beta</version>
<name>MyProject</name>
<packaging>jar</packaging>
<!-- DEPENDANCIES -->
<dependencies>
    <dependency>
    <groupId>custom</groupId>
    <artifactId>custom</artifactId>
    <version>1.0.0</version>
</dependency>    
    <dependency>
      <groupId>jgraphx</groupId>
      <artifactId>jgraphx</artifactId>
      <version>1.0.0</version>
</dependency>   
</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
<!-- RESOURCES -->
    <resources>
      <resource>
        <directory>src</directory>
        <filtering>true</filtering>
      </resource>
      <resource>
        <directory>${basedir}/images</directory>
    <filtering>false</filtering>
    <targetPath>images</targetPath>
      </resource>
      <resource>
        <directory>${basedir}/resources</directory>
    <filtering>false</filtering>
    <targetPath>resources</targetPath>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>src</directory>
      </testResource>
    </testResources>
    <plugins>
<!--  For Java 6, you need to configure the maven-compiler-plugin. Add this to your pom.xml: -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
<!-- CREATE EXECUTABLE JAR WITH DEPENDANCIES -->
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
        <mainClass>Main.Main</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id> <!-- this is used for inheritance merges -->
          <phase>package</phase> <!-- bind to the packaging phase -->
          <goals>
        <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    </plugins>
</build>
</project>

编辑:看起来它可能是我的代码。我去看看,谢谢!(我公司的代理阻止了大部分stackoverflow,所以我无法回复评论>。<这就是我在这里写它的原因。感谢Jigar Joshi和Aurand!)

4

1 回答 1

0

查看java构建路径。资源的默认输出文件夹可能不正确。可能这就是资源内容未正确复制的原因。

于 2013-10-21T12:36:54.303 回答