Maven:将依赖项和spring bean配置文件添加到jar文件中

时间:2013-12-18 12:54:34

标签: java spring maven jar

我创建了Spring Standalone Application,在该项目中,我有Java Classes,Spring配置文件(applicationContext.xml)和包含spring依赖项的pom.xml文件。

这是我项目的结构:

ExampleProject
|-- src/main/java
    |--org.example
       |--java Classes(MainApp.java)
|-- applicationContext.xml
|-- src/main/resource
|-- src
|-- target
|-- pom.xml

问题: 但我的问题是,当使用Maven Assembly插件创建jar文件时,它只添加maven依赖项和java类,但不添加spring配置文件。可以为我提供将依赖项和spring bean配置文件添加到的良好解决方案jar文件

以下是供您参考的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>com.iton.createjarProject</groupId>
<artifactId>createjarProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>
<dependencies>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

</dependencies>
<build>
        <plugins> <plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
  <execution>
    <id>unpack-dependencies</id>
    <phase>prepare-package</phase>
    <goals>
      <goal>unpack-dependencies</goal>
    </goals>
    <configuration>
      <outputDirectory>${project.build.directory}/classes</outputDirectory>
    </configuration>
  </execution>

</executions>
</plugin>


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
  <execution>
    <id>unpack-dependencies</id>
    <phase>package</phase>
  </execution>
 </executions>
 <configuration>
  <archive>
    <manifest>
      <addClasspath>true</addClasspath>
      <classpathPrefix>lib/</classpathPrefix>
      <mainClass>SimpleKeyLogger</mainClass>
    </manifest>
  </archive>
 </configuration>
</plugin>

        </plugins>


</build>


</project>

1 个答案:

答案 0 :(得分:2)

Maven convention是应用程序资源文件(例如Spring上下文文件)应放在src/main/resources中 - 除非已在pom.xml中明确重新定义。 Maven将拾取此文件夹中的任何内容,并在打包应用程序时将其放入工件中。

所以将您的applicationContext.xml移至src/main/resources(它应该被称为src / main / 资源而不是src / main / 资源)< / p>