我创建了 Spring 独立应用程序,在该项目中我有 Java 类、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>
最佳答案
Maven convention应用程序资源文件(例如 Spring 上下文文件)应放置在 src/main/resources 中 - 除非已在 pom.xml 中显式重新定义。打包应用程序时,Maven 将拾取此文件夹中的所有内容并将其放入 Artifact 中。
因此,将您的 applicationContext.xml
移动到 src/main/resources
(它应该被称为 src/main/resources 而不是 src/主要/资源)
关于java - Maven:将依赖项和spring bean配置文件添加到jar文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20658815/