Maven構建可運行的jar包出錯解決辦法

手上有個項目需要以jar方式運行,使用maven-shade-plugin插件構建成功後,在服務器上運行"nohup java -jar myProject.jar > /dev/null &"出錯,錯誤信息:“Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes”,網上搜索了半天,發現一個解決辦法:  http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar

在pom文件maven-shade-plugin插件的配置信息中添加:

<configuration>
    <filters>
        <filter>
            <artifact>*:*</artifact>
            <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
            </excludes>
        </filter>
    </filters>
    <!-- Additional configuration. -->
</configuration>

繼續運行,之前的錯誤解決了,卻提示新的錯誤:“Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context] Offending resource: class path resource [applicationContext.xml]

解決辦法是在構建的時候加入META-INF/spring.schemas  META-INF/spring.handlers transformers,最終的maven-shade-plugin插件信息配置如下:

<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-shade-plugin</artifactId>
			<version> 1.7.1</version>
			<executions>
				<execution> 
					<phase>package</phase>
					<goals>
						<goal>shade</goal>
					</goals>
					<configuration>
						<filters>
					        <filter>
					            <artifact>*:*</artifact>
					            <excludes>
					                <exclude>META-INF/*.SF</exclude>
					                <exclude>META-INF/*.DSA</exclude>
					                <exclude>META-INF/*.RSA</exclude>
					            </excludes>
					        </filter>
					    </filters>
						<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.AppendingTransformer">
								<resource>META-INF/spring.schemas</resource>
							</transformer>
							<transformer
								implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
								<mainClass>com.zhilin.paopao.server.NIOServer</mainClass>
							</transformer>
						</transformers>
					</configuration>
				</execution>
			</executions>
		</plugin>

重新clean install打包,再次運行之後問題解決~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章