当前位置:   article > 正文

Java(8):maven打包成可执行jar包(1个jar包和1个lib依赖包目录)_若依框架打包出来为什么是一个 lib包一个 jar 包

若依框架打包出来为什么是一个 lib包一个 jar 包

开使用maven-jar-plugin和maven-dependency-plugin打可执行包

开发工具:idea

插件版本:

1、maven-jar-plugin  3.0.2

2、maven-dependency-plugin 3.0.1

说明:(mvn package生成(一个jar包和一个lib文件夹(依赖jar包目录)))

插件maven-jar-plugin负责生成指定入口的可执行JAR文件。同时在manifest.mf文件里写入入口函数信息和classpath。本例中是吧classpath设置为lib目录。

插件maven-dependency-plugin负责创建lb目录,同时把需要依赖的jar包全部复制进去lib目录。

1.1 pop.xml

     <build>

        <plugins>

            <!-- 设置编译版本为1.8 -->

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

                <configuration>

                    <source>1.8</source>

                    <target>1.8</target>

                    <encoding>UTF-8</encoding>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-jar-plugin</artifactId>

                <version>3.0.2</version>

                <configuration>

                    <archive>

                        <manifest>

                            <!-- 不打包依赖的jar,把依赖的jar copy到lib目录,和生成的jar放在同一级目录下 -->

                            <addClasspath>true</addClasspath>

                            <mainClass>SDKTest</mainClass>

                            <classpathPrefix>lib/</classpathPrefix>

                        </manifest>

                        <!--不将 pom.xml ,pom.propeties等打包进jar包中-->
                        <addMavenDescriptor>false</addMavenDescriptor>

                    </archive>

                    <excludes>

                        <exclude>1*.p12</exclude>

                        <exclude>cg-casb.properties</exclude>

                    </excludes>

                </configuration>

            </plugin>

            <!-- Copy project dependency -->

            <!-- 将所依赖的第三方jar包copy入lib目录 -->

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-dependency-plugin</artifactId>

                <version>3.0.1</version>

                <executions>

                    <execution>

                        <id>copy-dependencies</id>

                        <phase>package</phase>

                        <goals>

                            <goal>copy-dependencies</goal>

                        </goals>

                        <configuration>

                            <!-- exclude junit, we need runtime dependency only -->

                            <includeScope>runtime</includeScope>

                            <outputDirectory>${project.build.directory}/lib/</outputDirectory>

                            <overWriteReleases>false</overWriteReleases>

                            <overWriteSnapshots>false</overWriteSnapshots>

                            <overWriteIfNewer>true</overWriteIfNewer>

                        </configuration>

                    </execution>

                </executions>

            </plugin>

        </plugins>

    </build>

1.2 jar执行文件

执行maven package 进行打包进入target目录。

其中lib就是第三方jar包的目录,在其他项目引入SdkTestDemo-1.0-SNAPSHOT.jar会自动找到并引入lib里面的包.

只需要拷贝这两个(lib和SdkTestDemo-1.0-SNAPSHOT.jar)即可放到其他地方用了。

在命令行执行:java -jar SdkTestDemo-1.0-SNAPSHOT.jar

会在MANIFEST.MF生成classpath: lib/。

执行时需要把lib文件夹(maven打包时target目录会有lib目录)拷贝到执行目录中(引用的第三方包放在执行目录的的lib下).

1.3执行

 执行步骤:java -jar  xxx.jar包

其他:

1、Maven打包排除某个资源或者目录

排除某个资源文件

<plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-jar-plugin</artifactId>

                <version>3.0.2</version>

                <configuration>

                    <excludes>

                        <exclude>1*.p12</exclude>

                        <exclude>cg-casb.properties</exclude>

                    </excludes>

                </configuration>

            </plugin>

排除某个目录

<includes>

            <include>**/service/*</include>

</includes>

详细参考官方文档:https://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html

参考:

maven 将本地jar包打入jar包中_朝闻道_的博客-CSDN博客

2、排除:不将 pom.xml ,pom.propeties等打包进jar包中

加上这句:<addMavenDescriptor>false</addMavenDescriptor>

           <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-jar-plugin</artifactId>

                <version>3.0.2</version>

                <configuration>

                    <archive>

                        <manifest>

                            <!-- 不打包依赖的jar,把依赖的jar copy到lib目录,和生成的jar放在同一级目录下 -->

                            <addClasspath>true</addClasspath>

                            <mainClass>SDKTest</mainClass>

                            <classpathPrefix>lib/</classpathPrefix>

                        </manifest>

                        <addMavenDescriptor>false</addMavenDescriptor>

                    </archive>

                    <excludes>

                        <exclude>1*.p12</exclude>

                        <exclude>cg-casb.properties</exclude>

                    </excludes>

                </configuration>

            </plugin>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/308076
推荐阅读
相关标签
  

闽ICP备14008679号