spring.xml not packaged in executable jar

440 views Asked by At

I created a simple java application using maven, spring and used log4j for logging.

Following are the files.

    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>

I have a spider class and other related classes that are used in the application. When i run this project from within eclipse, it works fine. The problem is that when i create a executable jar, spring.xml and log4j.xml are not included in jar. What could be the reason? What could be the possible solution? When i run the jar, it give me a FileNotFoundException since the files are not available in jar.

directory structure

1

There are 1 answers

0
sarumait On BEST ANSWER

If you're using maven, you can put your XML files in src/main/resources. By default, all files placed in the resources folder will be included in the jar file and will be accessible by your application.