spring.xml没有打包在可执行jar中

时间:2015-06-16 18:10:01

标签: java spring maven executable-jar

我使用maven,spring创建了一个简单的java应用程序,并使用log4j进行日志记录。

以下是文件。

    public class TestCrawler {

    public static final String SPRING_CONFIGURATION_FILE = "spring.xml";
    private static final String INITIAL_URL = "http://yahoo.com/"; 
    private static final String SPIDER_BEAN = "spider";

    public static void main(String[] args) {
      System.out.println();
        ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_CONFIGURATION_FILE); 
        Spider spider = (Spider) ctx.getBean(SPIDER_BEAN);
        spider.startCrawling(INITIAL_URL);
    }
}

的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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>recruiterbox</groupId>
  <artifactId>webcrawler</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <name>WebCrawler</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jdk.version>1.7</jdk.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.7.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
  </dependencies>

  <build>
      <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                      <manifest>
                        <mainClass>recruiterbox.webcrawler.TestCrawler</mainClass>
                      </manifest>
                    </archive>
                </configuration>
                <executions>
                  <execution>
                    <id>make-assembly</id>
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>
      </plugins>
    </build>
</project>

我有一个蜘蛛类和应用程序中使用的其他相关类。当我从eclipse中运行这个项目时,它运行正常。 问题是,当我创建一个可执行jar时,jar中不包含spring.xml和log4j.xml。 可能是什么原因?可能的解决方案是什么?当我运行jar时,它会给我一个FileNotFoundException,因为文件在jar中不可用。

directory structure

1 个答案:

答案 0 :(得分:0)

如果您正在使用maven,则可以将XML文件放在src / main / resources中。默认情况下,放在resources文件夹中的所有文件都将包含在jar文件中,并且您的应用程序可以访问它们。

相关问题