Unable to add resources to final jar for maven project in Intellij

天涯浪子 提交于 2021-02-08 11:46:37

问题


I am unable to add images from my resources directory (which is marked as resources root) to the final jar that I want t generate. I have tried building a jar using File -> Project Structure -> Project Settings -> Artifacts -> Click green plus sign -> Jar -> From modules with dependencies. But failed. I have also tried building a jar from the Maven project window in Intellij. Which failed as well.

Here is my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<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>

<groupId>com.sample.project</groupId>
<artifactId>login</artifactId>
<version>1.0-SNAPSHOT</version>


<build>
    <resources>
        <resource>
            <directory>
                src/main/resources
            </directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.sample.project.Login</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

My project structure looks like this: Project structure

zip file of my sample project: Archive


回答1:


<build>
        <finalName>${project.artifactId}</finalName>
        <defaultGoal>dependency:sources</defaultGoal>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>

        </resources>

Above code in pom.xml would copy xml files located in resources. You can change to some other files or remove

                <includes>
                    <include>**/*.xml</include>
                </includes>

will copy all files in resources.




回答2:


The files that you have in the directory src/resources/ need to be moved to src/main/resources/, otherwise Maven will ignore them.

Additionally, remove this

    <resources>
        <resource>
            <directory>
                src/main/resources
            </directory>
        </resource>
    </resources>

because this is the default convention that Maven will use anyway.



来源:https://stackoverflow.com/questions/49808337/unable-to-add-resources-to-final-jar-for-maven-project-in-intellij

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!