您的当前位置:首页正文

spring 项目如何打包

2024-11-11 来源:个人技术集锦

一般现在是使用maven 项目进行管理,对于有配置文件的spring 项目(web项目直接打包成war文件就可以了),如何打包成一个可以执行的jar
我们可以使用maven的插件,具体就是像下面这样在pom.xml 中添加maven-shade插件就可以了

<plugins>
       <plugin>  
      <groupId>org.apache.maven.plugins</groupId>  
      <artifactId>maven-shade-plugin</artifactId>  
      <version>1.7</version>  
      
      <executions>  
        <execution>  
          <phase>package</phase>  
          <goals>  
            <goal>shade</goal>  
          </goals>  
          <configuration>  
            <finalName>my-spring-app</finalName>  
            <shadedArtifactAttached>true</shadedArtifactAttached>  
            <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
              
            <transformers>  
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                <mainClass>com.webmagic.processor.XinhuaProcessor</mainClass>  
              </transformer>  
              <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                <resource>META-INF/spring.handlers</resource>  
              </transformer>
              <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                <resource>META-INF/spring.schemas</resource>  
              </transformer>  
              <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                <resource>META-INF/spring.tooling</resource>  
              </transformer>  
            </transformers>  
            
       
          </configuration>  
        </execution>  
      </executions>  
    </plugin>  
        </plugins>
一般我们还有很多的配置文件像*.properties,*.xml等,这个可以在pom.xml中添加一个resources就可以了,如下

<!-- 添加资源文件 -->
    	<resource>  
            <directory>src/main/resource</directory>  
            <includes>  
                <include>*.properties</include>  
                <include>*.xml</include>
                <include>mapping/*.xml</include>  
            </includes>  
            <filtering>false</filtering>  
        </resource>  
       </resources>
这样就可以保证在我们打包成jar文件时也能够带上配置文件,能够在项目中找到。

Top