用maven打包含依赖的jar包

版权声明:分享,转载原创文章,麻烦注明一下出处~谢谢 https://blog.csdn.net/sc9018181134/article/details/78948236

今天给同学做一个excel处理的小工具的时候,因为同学不懂编程,又不想安装过多的环境什么的,就决定采用jar包的形式,让他在cmd中运行这个小工具.

遇到的问题:直接生成jar包,并且运行的是jar中的main方法.
采取的解决方式:用maven生成包含依赖的jar包,并且指定运行jar的时候,用哪一个方法作为程序的入口.

1.在maven项目中的pom.xml添加插件maven-assembly-plugin

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>baidu.data.api</groupId>
    <artifactId>scott.test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>scott.test</name>
    <url>http://maven.apache.org</url>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

             <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <!-- mainClass用来指定main方法入口 -->
                            <mainClass>excelUtil.ExcelUtil</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <!-- 下面都是需要依赖的jar包 -->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>2.2.3</version>
        </dependency>

        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.4</version>
        </dependency>


        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.2.9</version>
        </dependency>

    </dependencies>




</project>

并且在mainClass 中指定程序的入口(这里是main方法).
2.然后使用mvn clean package 进行打包.将会在项目的target中生成一个jar包.
3.最后使用java -jar XXX.jar,运行即可.

可能会遇到的坑:
1.maven默认编译版本是1.4,所以使用1.4以上的版本的jdk写的代码,用maven编译会报错,类似“maven 不支持1.7或1.8”,点击-参考我的转载的一篇文章

2.注:有的人编码方式没有设置过,会报”编码GBK的不可映射字符(类似这样的)”,maven默认是根据不同平台使用不同的编码,window下是GBK,所以需要在pom.xml中添加

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <!-- 这里指定编码方式就不会报错了-->
                    <encoding>UTF-8</encoding>
                </configuration>
</plugin>

3.可能有报错:找不到主类和没有主清单属性的解决,主要是因为在maven-assembly里面没有指定mainClass.

猜你喜欢

转载自blog.csdn.net/sc9018181134/article/details/78948236
今日推荐