首页 > 工作 > Maven打包Spring项目成单独文件

Maven打包Spring项目成单独文件

Maven打包项目成单独文件,一般用maven-assembly-plugin插件,一般配置如下:

			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>

				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<descriptors>
						<descriptor>src/main/assemble/package.xml</descriptor>
					</descriptors>
					<archive>
						<manifest>
							<mainClass>*.*.*.Main</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>assembly</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

碰到Spring项目,就容易出问题:

Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace

各种查找确认,发现是把Spring的两个文件打包错了。META-INF/spring.handlers和META-INF/spring.schemas中内容不全。
原因是maven-assembly-plugin插件在合并jar包时,会采用覆盖的方式存储这两个文件。
改用maven-shade-plugin即可解决此问题,把这两个文件配置为追加合并方式存储:

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-shade-plugin</artifactId>  
    <version>1.4</version>  
    <executions>  
        <execution>  
            <phase>package</phase>  
            <goals>  
                <goal>shade</goal>  
            </goals>  
            <configuration>  
                <transformers>  
                    <transformer  
                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                        <resource>META-INF/spring.handlers</resource>  
                    </transformer>  
                    <transformer  
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                        <mainClass>*.*.*.Main</mainClass>  
                    </transformer>  
                    <transformer  
                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                        <resource>META-INF/spring.schemas</resource>  
                    </transformer>  
                </transformers>  
            </configuration>  
        </execution>  
    </executions>  
</plugin>
  1. 本文目前尚无任何评论.