Java Maven:在jar文件中添加依赖和spring bean配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20658815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:23:03  来源:igfitidea点击:

Maven:adding dependencies and spring bean configuration file into the jar file

javaspringmavenjar

提问by user2323555

I created the Spring Standalone Application,In that project i have the Java Classes,Spring Configuration File(applicationContext.xml) and the pom.xml file which contains the spring dependencies.

我创建了 Spring 独立应用程序,在该项目中我有 Java 类、Spring 配置文件(applicationContext.xml)和包含 spring 依赖项的 pom.xml 文件。

And this is the Structure of my project:

这是我项目的结构:

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

Problem:But my problem is when creating the jar file using the Maven Assembly plug-in it is adding only the maven dependencies and java class but not the spring configuration file.Can you provide me the good solution for adding the dependencies and spring bean configuration file into the jar file

问题:但是我的问题是在使用Maven Assembly插件创建jar文件时它只添加了maven依赖和java类,而不是spring配置文件。你能给我提供添加依赖和spring bean配置的好的解决方案吗?将文件放入 jar 文件

Below is the pom.xml for your reference

下面是 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>

回答by Will Keeling

The Maven conventionis that application resource files (such as the Spring context file) should be placed in src/main/resources- unless this has been explicitly redefined in the pom.xml. Maven will pick up anything in this folder and place it into the artifact when packaging the app.

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

So move your applicationContext.xmlto src/main/resources(and it should be called src/main/resourcesnot src/main/resource)

所以移动你applicationContext.xmlsrc/main/resources(它应该被称为 src/main/ resources而不是 src/main/ resource