zoukankan      html  css  js  c++  java
  • mvn生成runnablejar 的方法

    主要讲3点,生成runnable jar 方法1是生成一个目录 方法2是直接一个runnable的jar 方法3是关于包含spring工程的情况  方法2和3其实是一致的

    1、生成runnable jar,但是最后并不是一个可执行的jar 而是一个目录

    主要使用 三个插件 将所需要的资源拷贝到 /target/Crunchify目录下,注意并没有生成一个jar中去

    maven-resources-pluginmaven-dependency-plugin & maven-jar-plugin to generate your complete executable Jar Project? As a result it creates / copies all required files to /target/Crunchify folder.

    几点说明:

    1. CrunchifyMavenBuildPlugins is a Maven Project. If you have Java project and wanted to convert it into Maven project then follow this tutorial.
    2. We do have two folders. src and resources.
    3. Inside resources folder we do have a folder called Scripts which contains one executableshell script file.
    4. CrunchifyMain.java is a main starting point which has main(String args[]) method inside.
    5. pom.xml file in which we will add Maven Plugins which will build executable .jar project with all included dependancies

    pom文件如下:注意build标签下的3个plugins<build>

     1   <pluginManagement>
     2             <plugins>
     3                 <plugin>
     4                     <groupId>org.apache.maven.plugins</groupId>
     5                     <artifactId>maven-compiler-plugin</artifactId>
     6                     <version>2.3.1</version>
     7                     <configuration>
     8                         <source>1.7</source>
     9                         <target>1.7</target>
    10                     </configuration>
    11                 </plugin>
    12             </plugins>
    13         </pluginManagement>
    14         <plugins>
    

    <!--说明:The Resources Plugin 会将project resources拷贝到输出目录.project resources are the resources associated to the main source code-->
    <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
     <executions>
     <execution>
     <id>copy-resources</id>
    <phase>validate</phase>
    <goals>
     <goal>copy-resources</goal>
    </goals>
    <configuration>
    <outputDirectory>${basedir}/target/Crunchify</outputDirectory>
     <resources>
    <resource>
    <directory>resources</directory>
    <filtering>true</filtering>
    </resource>
    </resources>
    </configuration>
    </execution>
    </executions>
    </plugin>
     
      
                 <plugin>
                     <groupId>org.apache.maven.plugins</groupId>
                     <artifactId>maven-dependency-plugin</artifactId>
     <!--说明:The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location-->
                    <executions>
                         <execution>
                             <id>copy-dependencies</id>
                             <phase>prepare-package</phase>
                             <goals>
                                 <goal>copy-dependencies</goal>
                             </goals>
                            <configuration>
                                 <outputDirectory>${project.build.directory}/Crunchify/lib</outputDirectory>
                                 <overWriteReleases>false</overWriteReleases>
                                 <overWriteSnapshots>false</overWriteSnapshots>
                                 <overWriteIfNewer>true</overWriteIfNewer>
                             </configuration>
                         </execution>
                     </executions>
                 </plugin>
                 <plugin>
                     <groupId>org.apache.maven.plugins</groupId>
                     <artifactId>maven-jar-plugin</artifactId>
     <!-- This plugin provides the capability to build and sign jars.-->
                     <configuration>
                         <archive>
                             <manifest>
                                 <addClasspath>true</addClasspath>
                                 <classpathPrefix>lib/</classpathPrefix>
                                 <mainClass>com.crunchify.tutorial.CrunchifyMain</mainClass>
                             </manifest>
                             <manifestEntries>
                                 <Class-Path>.</Class-Path>
                             </manifestEntries>
                         </archive>
      
                         <finalName>Crunchify/Crunchify</finalName>
                     </configuration>
                 </plugin>
             </plugins>
         </build>

     最后看看结果

    2、生成到一个jar包中,这个时候需要在插件maven-shade-plugin上做文章,主要用到的技术是 Resource Transformers

    主要看下pom文件

     81     <build>
     82         <pluginManagement>
     83             <plugins>
     84                 <plugin>
     85                     <groupId>org.apache.maven.plugins</groupId>
     86                     <artifactId>maven-compiler-plugin</artifactId>
     87                     <version>2.3.1</version>
     88                     <configuration>
     89                         <source>1.7</source>
     90                         <target>1.7</target>
     91                     </configuration>
     92                 </plugin>
     93             </plugins>
     94         </pluginManagement>
     95         <plugins>
     96             <plugin>
     97                 <groupId>org.apache.maven.plugins</groupId>
     98                 <artifactId>maven-shade-plugin</artifactId>
    <!-- 主要使用了Resource Transformers 做 Aggregating classes/resourcesfrom several artifacts into one jar-->
    99 <version>1.7</version> 100 <executions> 101 <execution> 102 <phase>package</phase> 103 <goals> 104 <goal>shade</goal> 105 </goals> 106 <configuration> 107 <!-- Optional Start --> 108 <finalName>Crunchify</finalName> 109 <shadedArtifactAttached>true</shadedArtifactAttached> 110 <shadedClassifierName>jar-with-dependencies</shadedClassifierName> 111 <!-- Optional End --> 112 113 <transformers> 114 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 115 <mainClass>com.crunchify.tutorial.CrunchifyMain</mainClass> 116 </transformer> 117 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 118 <resource>META-INF/spring.handlers</resource> 119 </transformer> 120 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 121 <resource>META-INF/spring.schemas</resource> 122 </transformer> 123 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 124 <resource>META-INF/spring.tooling</resource> 125 </transformer> 126 </transformers> 127 </configuration> 128 </execution> 129 </executions> 130 </plugin>
    131 132 </plugins> 133 </build> 134 </project>

     3、一般的,如果是spring工程,打成可执行包的话在读取xml文件时会报错,这个时候需要在mvn中做些处理,生成一个runnable的jar,直接用mvn添加一些选项。这里其实也是使用了resouce transform

    <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>pc-guess-like-fsf-app</finalName>  //1、可执行jar包名字
            <shadedArtifactAttached>true</shadedArtifactAttached>
            <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
                <mainClass>com.feiniu.recom.soa.consumer.pcGuessLike.FsfTest</mainClass>       //2、这里是可执行jar包主类名字
              </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>
     
    我当记事本用的
  • 相关阅读:
    HTML5 重力感应效果,实现摇一摇效果
    WEB 移动端 CSS3动画性能 优化
    jquery 插件封装模板
    textarea 提交到数据库的内容,输出到 html 中显示正常的格式
    js根据银行卡号判断属于哪个银行,并返回银行缩写及银行卡类型
    微信小程序如何引用iconfont图标
    nodejs: express basic
    javascript设计模式:适配器模式
    javascript设计模式:装饰者模式
    javascript设计模式:代理模式
  • 原文地址:https://www.cnblogs.com/amazement/p/4870833.html
Copyright © 2011-2022 走看看