简体   繁体   中英

Maven: Not copying files from src/main/resources to the classpath root - Executable Jar

I am building an executable jar with Maven using the Assembly plugin. I have a resource file(.xml) which is placed at src/main/resources. When I build the executable jar, the file is not getting copied into the jar - Checked by unpacking the jar.

Here is my 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>

I am trying to call the following resource which is kept under src/main/resources:

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

Getting the following exception while executing 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)

Has anyone faced a similar issue before? Looking for your help, Maven gurus..

You can try replacing your Configuration as follows;

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

And then

mvn package 

By default, maven-assembly-plugin makes TWO jars, not one (with phase package). Make sure you are running the one called <artifactid>-<version>-jar-with-dependencies.jar

If this is not the problem, are you doing anything weird in your pom here:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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