Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Maven程序集插件:资源被复制到jar的根目录下_Java_Maven_Jar - Fatal编程技术网

Java Maven程序集插件:资源被复制到jar的根目录下

Java Maven程序集插件:资源被复制到jar的根目录下,java,maven,jar,Java,Maven,Jar,我正在使用maven汇编插件构建java8项目,并将其打包为JAR文件 我在src/main/resources(通常的maven资源目录)中找到了一些资源。 创建JAR时,src/main/resources下的所有资源文件都复制到JAR的根目录下 在代码中,我尝试使用FileInputStream打开文件,这些文件位于src/main/resources中,在运行项目时,如果不将其打包为JAR文件,效果会非常好。然而,当我从JAR文件运行项目时,我会得到许多FileNotFoundExcep

我正在使用maven汇编插件构建java8项目,并将其打包为JAR文件

我在
src/main/resources
(通常的maven资源目录)中找到了一些资源。 创建JAR时,
src/main/resources
下的所有资源文件都复制到JAR的根目录下

在代码中,我尝试使用
FileInputStream
打开文件,这些文件位于
src/main/resources
中,在运行项目时,如果不将其打包为JAR文件,效果会非常好。然而,当我从JAR文件运行项目时,我会得到许多
FileNotFoundException
,因为我的资源不再位于
src/main/resources
,而是位于
${project.basedir}

我希望它们位于JAR文件中的
src/main/resources
。可能吗

我的构建配置如下所示:

   <build>
        <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>

        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>

                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>EmployProject.TestAlgo</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>

                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>TestAlgo</mainClass>
                        </manifest>
                    </archive>

                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

        </plugins>

        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

    </build>

${project.basedir}/src/main/java
org.apache.maven.plugins
maven jar插件
3.0.2
真的
解放党/
EmployProject.TestAlgo
带有依赖项的jar
maven汇编插件
包裹
单一的
真的
解放党/
特斯塔尔戈
带有依赖项的jar
${project.basedir}/src/main/resources
**/*.xml
编辑


如果不可能,在我的代码中,我应该使用什么来访问我的资源文件?

感谢@khmarbaise和本文:

我发现我使用了错误的API来打开资源。 JAR中的资源不是文件,因此我更改了:

XMLDecoder xmlDec = new XMLDecoder(new InputFileStream(this.getClass().getResourceAsStream(fileName))
致:


嗯,是的,我为什么要用它?它的好处是什么?它将如何解决我的问题?我的资源文件包含在JAR中。问题是它们被放在我的项目的根目录下,当我在代码中尝试使用getClass().getResource(fileName)访问它们时,它返回null,因为它找不到文件。首先删除的
sourceDirectory
和资源配置都是默认值…所以不要配置它…除此之外,默认情况下,资源位于jar文件的根目录,这意味着您必须使用
getResourceAsStream(“/property.properties”)
InputStream in = EnumTools.class.getResourceAsStream(fileName);
XMLDecoder decoder = new XMLDecoder(in);