Maven打jar包时没有把pom.xml中的依赖打入jar包中,导致项目运行出错

  1. 问题
    当在IDEA中写完项目,准备打包时,在右侧工具栏选中Maven—>项目名下的Lifecycle—>clean—>package一系列打包步骤时
    已经完成了打包流程,打包的jar包会生成在项目下target目录里,当你在控制台执行jar包时(这里比如我打的jar包名为test1.jar)
    就会报错如下,类无法加载异常没有找到类错误

    1
    2
    3
    4
    5
    6
    7
    8
    9
    java -jar test.jar
    Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
    at top.lete114.autorenew.main.main(main.java:22)
    Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 1 more
  2. 解决:
    pom.xml里添加maven插件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    <build>
    <plugins>
    <plugin>
    <!--<groupId>org.apache.maven.plugins</groupId>-->
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>

    <plugin>
    <artifactId>maven-assembly-plugin </artifactId>
    <configuration>
    <descriptorRefs>
    <!-- 此处填写打包后jar包后添加的标识 -->
    <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
    <manifest>
    <!-- 此处填写程序的主入口(main方法) -->
    <mainClass>top.lete114.autorenew.main</mainClass>
    </manifest>
    </archive>
    </configuration>
    <executions>
    <execution>
    <id>make-assembly</id>
    <phase>package</phase>
    <goals>
    <goal>single</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>

    完整pom.xml代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    <?xml version="1.0" encoding="UTF-8"?>
    <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>top.lete114.autorenew</groupId>
    <artifactId>AutoRenew</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
    <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
    <dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.13.1</version>
    </dependency>
    </dependencies>

    <build>
    <plugins>
    <plugin>
    <!--<groupId>org.apache.maven.plugins</groupId>-->
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>

    <plugin>
    <artifactId>maven-assembly-plugin </artifactId>
    <configuration>
    <descriptorRefs>
    <!-- 此处填写打包后jar包后添加的标识 -->
    <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
    <manifest>
    <!-- 此处填写程序的主入口(main方法) -->
    <mainClass>top.lete114.autorenew.main</mainClass>
    </manifest>
    </archive>
    </configuration>
    <executions>
    <execution>
    <id>make-assembly</id>
    <phase>package</phase>
    <goals>
    <goal>single</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>
    </project>

配置后好更新maven依赖后再次cleanpackage即可在target目录里多出一个jar包
其中标有jar-with-dependencies是打入pom.xml依赖的jar包
为标有jar-with-dependencies是没有依赖的jar包

文章作者: Lete乐特
文章链接: https://blog.lete114.top/article/Maven-Package.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Lete乐特's Blog
  • alipay
  • wechat

评论