• maven 打包可执行jar的方法


    1.修改pom.xml增加如下内容

    <plugin>  
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-jar-plugin</artifactId>  
        <version>2.4</version>  
        <configuration>  
            <archive>  
                <manifest>  
                    <addClasspath>true</addClasspath>  
                    <classpathPrefix>lib/</classpathPrefix>  
                    <mainClass>com.sysware.HelloWorld</mainClass>  
                </manifest>  
            </archive>  
        </configuration>  
    </plugin>  

    运行mvn clean package即可

    2.在pom.xml增加如下内容

    <plugin>  
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-assembly-plugin</artifactId>  
        <version>2.3</version>  
        <configuration>  
            <appendAssemblyId>false</appendAssemblyId>  
            <descriptorRefs>  
                <descriptorRef>jar-with-dependencies</descriptorRef>  
            </descriptorRefs>  
            <archive>  
                <manifest>  
                    <mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>  
                </manifest>  
            </archive>  
        </configuration>  
        <executions>  
            <execution>  
                <id>make-assembly</id>  
                <phase>package</phase>  
                <goals>  
                    <goal>assembly</goal>  
                </goals>  
            </execution>  
        </executions>  
    </plugin>  

    运行mvn assembly:assembly

    <build>  
            <finalName>...</finalName>  
            <sourceDirectory>src/main/java</sourceDirectory>  
            <resources>  
                <!-- 控制资源文件的拷贝 -->  
                <resource>  
                    <directory>src/main/resources</directory>  
                    <targetPath>${project.build.directory}</targetPath>  
                </resource>  
            </resources>  
            <plugins>  
                <!-- 设置源文件编码方式 -->  
                <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-compiler-plugin</artifactId>  
                    <configuration>  
                        <defaultLibBundleDir>lib</defaultLibBundleDir>  
                        <source>1.6</source>  
                        <target>1.6</target>  
                        <encoding>UTF-8</encoding>  
                    </configuration>  
                </plugin>  
                <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->  
                <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-jar-plugin</artifactId>  
                    <configuration>  
                        <archive>  
                            <manifest>  
                                <addClasspath>true</addClasspath>  
                                <classpathPrefix>lib/</classpathPrefix>  
                                <mainClass>.....MonitorMain</mainClass>  
                            </manifest>  
                        </archive>  
                    </configuration>  
                </plugin>  
                <!-- 拷贝依赖的jar包到lib目录 -->  
                <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-dependency-plugin</artifactId>  
                    <executions>  
                        <execution>  
                            <id>copy</id>  
                            <phase>package</phase>  
                            <goals>  
                                <goal>copy-dependencies</goal>  
                            </goals>  
                            <configuration>  
                                <outputDirectory>  
                                    ${project.build.directory}/lib  
                                </outputDirectory>  
                            </configuration>  
                        </execution>  
                    </executions>  
                </plugin>  
                <!-- 解决资源文件的编码问题 -->  
                <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-resources-plugin</artifactId>  
                    <version>2.3</version>  
                    <configuration>  
                        <encoding>UTF-8</encoding>  
                    </configuration>  
                </plugin>  
                <!-- 打包source文件为jar文件 -->  
                <plugin>  
                    <artifactId>maven-source-plugin</artifactId>  
                    <version>2.1</version>  
                    <configuration>  
                        <attach>true</attach>  
                        <encoding>UTF-8</encoding>  
                    </configuration>  
                    <executions>  
                        <execution>  
                            <phase>compile</phase>  
                            <goals>  
                                <goal>jar</goal>  
                            </goals>  
                        </execution>  
                    </executions>  
                </plugin>  
            </plugins>  
        </build>  
    

      

    <build>  
        <resources>  
            <resource>  
                <targetPath>${project.build.directory}/classes</targetPath>  
                <directory>src/main/resources</directory>  
                <filtering>true</filtering>  
                <includes>  
                    <include>**/*.xml</include>  
                </includes>  
            </resource>  
        </resources>  
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.0</version>  
                <configuration>  
                    <source>1.6</source>  
                    <target>1.6</target>  
                    <encoding>UTF-8</encoding>  
                </configuration>  
            </plugin>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-shade-plugin</artifactId>  
                <version>2.0</version>  
                <executions>  
                    <execution>  
                        <phase>package</phase>  
                        <goals>  
                            <goal>shade</goal>  
                        </goals>  
                        <configuration>  
                            <transformers>  
                                <transformer  
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                                    <mainClass>com.test.testguava.app.App</mainClass>  
                                </transformer>  
                                <transformer  
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                                    <resource>applicationContext.xml</resource>  
                                </transformer>  
                            </transformers>  
                            <shadedArtifactAttached>true</shadedArtifactAttached>  
                            <shadedClassifierName>executable</shadedClassifierName>  
                        </configuration>  
                    </execution>  
                </executions>  
            </plugin>  
        </plugins>  
    </build>  
  • 相关阅读:
    Centos5.x 更换yum源
    前端常用的数据处理(基础版)
    动态库和静态库的区别是什么(转)
    java8.0_181b13 异常信息
    sql server 跨数据库调用存储过程
    基于51单片机的简易“视频播放器”
    https还安全吗 https抓包原理 https抓包工具
    hash 碰撞的2种解决办法
    商品秒杀提高QPS解决方案
    指针压缩原理和为什么指针压缩只能压缩3位
  • 原文地址:https://www.cnblogs.com/zhangjianbin/p/7169480.html
Copyright © 2020-2023  润新知