mvn包不包含xml

时间:2016-04-14 09:24:22

标签: java xml maven

我正在使用mybatis 3.3.1版本并将mybatis配置文件mybatis-conf.xml放在位于SqlSessionFactoryManager目录的位置。

我通过mvn package创建了可执行文件jar,但是SqlSessionFactoryManager告诉了错误

  

java.io.IOException:找不到资源kr / stocktalk / mybatis / mybatis-conf.xml   
在org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)

这是SqlSessionFactoryManager

的代码段
try {

    String resource = "kr/stocktalk/mybatis/mybatis-conf.xml";
    Reader reader = Resources.getResourceAsReader(resource);

    if (sqlSessionFactory == null) {
        sqlSessionFactory = new SqlSessionFactoryBuilder()
                .build(reader);
    }
} catch (Exception e) {
        e.printStackTrace();
}

我认为mybatis-conf.xml未包含在executable-jar

中的异常发生

所以我尝试在

中添加 include 代码maven-jar-plugin
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
    <excludes>
        <exclude>logback.xml</exclude>
        </excludes>
    <includes>
        <include>**/*.xml</include>
        <include>**/*.class</include>
    </includes>
    <archive>
        <manifest>
            <mainClass>kr.stocktalk.chat.ChatServer</mainClass>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin>

但是可执行文件jar文件中没有包含xml文件。

如何包含* .xml文件?

以下是所有pom.xml代码。

<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">
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <modelVersion>4.0.0</modelVersion>
    <groupId>kr.stocktalk</groupId>
    <artifactId>stocktalk-chat</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>kr.stocktalk.chat.ChatServer</mainClass>
                    <!-- <arguments> <argument>foo</argument> <argument>bar</argument> </arguments> -->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <!-- <compilerArgument>-Xlint:all</compilerArgument> -->
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- eclipse 에서는 target 밑의 lib 에 생성 됨 -->
                    <outputDirectory>target/lib</outputDirectory>
                    <overWriteIfNewer>true</overWriteIfNewer>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>logback.xml</exclude>
                    </excludes>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.class</include>
                    </includes>
                    <archive>
                        <manifest>
                            <mainClass>kr.stocktalk.chat.ChatServer</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${basedir}/target/resources</targetPath>
            </resource>
            <!-- <resource>
                <directory>src/main/java</directory>
                <targetPath>${basedir}/target</targetPath>
                <excludes>
                    <exclude>*.java</exclude>
                </excludes>
            </resource> -->
        </resources>

    </build>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-core</artifactId>
            <version>1.10.68</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-datapipeline</artifactId>
            <version>1.10.68</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-dynamodb</artifactId>
            <version>1.10.68</version>
        </dependency>
        <dependency>
            <groupId>com.ganyo</groupId>
            <artifactId>gcm-server</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.0.36.Final</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.8</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.jtds</groupId>
            <artifactId>jtds</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>
</project>

1 个答案:

答案 0 :(得分:1)

可以使用绝对或相对路径访问资源。您的路径range()是一个相对路径(相对于它所使用的类) - 但它看起来更像是您想要指定绝对资源路径。

因此,如果您的"kr/stocktalk/mybatis/mybatis-conf.xml"位于包SqlSessionFactoryManager中,则会尝试加载资源kr.stocktalk.mybatis

您应该通过在开头添加斜杠来使资源路径成为绝对路径:

kr.stocktalk.mybatis.kr.stocktalk.mybatis.mybatis-conf.xml