mvn assembly package jar 无法运行

       项目中要跑了一个jar工程,每天做一次数据结算。最开始我用的maven-assembly-plugin 打包,发现没什么问题。因为工程中引入了spring,有一处代码需要添加事务,所以就在application中引入了tx。再次打包,运行,提示tx schema 无法加载。百度了一下,因为spring  jar中包含了meta/spring.schema 和 spring.handlers,工程启动会通过存在的配置加载命名空间,但是和我需要的却不是一个版本,所以就会找不到对应的命名空间。 

      又是再次百度,找到了maven-shade-plugin ,到官网看了一下它的使用方式,mvn package 就可以了。

看了一下example ,发现竟然有这样一段配置

    Some jars contain additional resources (such as properties files) that have the same file name. To avoid overwriting, you can opt to merge them by appending their content into one file. One good example for this is when aggregating both the spring-context and plexus-spring jars. Both of them have the META-INF/spring.handlers file which is used by Spring to handle XML schema namespaces. You can merge the contents of all the files with that specific name using the AppendingTransformer as shown below:

 

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</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.AppendingTransformer">
                  <resource>META-INF/spring.schemas</resource>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

   
    英文注释 和我百度的解释类似 ,直接copy到了工程pom.xml 中 mvn package 然后java -jar  *.jar  运行正常
   

猜你喜欢

转载自zld406504302.iteye.com/blog/2206570