• maven中把依赖的JAR包一起打包


    一、scope要修改为runtime

    特别注意<scope>provided</scope>,只在编译的classpath中加载和使用,打包的时候不会包含在目标包中。 

    所以如果想把该依赖包也一起打包,那么应该改成<scope>runtime</scope>。

    具体可以参考:MAVEN Scope使用

    二、在pom.xml中添加maven-assembly-plugin插件

    
    
    1. <build>
    2. <plugins>
    3. <plugin>
    4. <artifactId> maven-assembly-plugin </artifactId>
    5. <configuration>
    6. <descriptorRefs>
    7. <descriptorRef>jar-with-dependencies</descriptorRef>
    8. </descriptorRefs>
    9. <archive>
    10. <manifest>
    11. <mainClass></mainClass>
    12. </manifest>
    13. </archive>
    14. </configuration>
    15. <executions>
    16. <execution>
    17. <id>make-assembly</id>
    18. <phase>package</phase>
    19. <goals>
    20. <goal>assembly</goal>
    21. </goals>
    22. </execution>
    23. </executions>
    24. </plugin>
    25. </plugins>
    26. </build>

     完整的pom.xml文件配置如下:

    
    
    1. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0</modelVersion>
    4.  
    5. <groupId>Test</groupId>
    6. <artifactId>test</artifactId>
    7. <version>0.0.1-SNAPSHOT</version>
    8. <packaging>jar</packaging>
    9.  
    10. <name>test</name>
    11. <url>http://maven.apache.org</url>
    12.  
    13. <properties>
    14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    15. </properties>
    16.  
    17. <dependencies>
    18. <dependency>
    19. <groupId>junit</groupId>
    20. <artifactId>junit</artifactId>
    21. <version>3.8.1</version>
    22. <scope>test</scope>
    23. </dependency>
    24.  
    25. <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.11 -->
    26. <dependency>
    27. <groupId>org.apache.spark</groupId>
    28. <artifactId>spark-core_2.11</artifactId>
    29. <version>2.1.1</version>
    30. <scope>runtime</scope>
    31. </dependency>
    32.  
    33. </dependencies>
    34.  
    35. <build>
    36. <plugins>
    37. <plugin>
    38. <artifactId> maven-assembly-plugin </artifactId>
    39. <configuration>
    40. <descriptorRefs>
    41. <descriptorRef>jar-with-dependencies</descriptorRef>
    42. </descriptorRefs>
    43. <archive>
    44. <manifest>
    45. <mainClass></mainClass>
    46. </manifest>
    47. </archive>
    48. </configuration>
    49. <executions>
    50. <execution>
    51. <id>make-assembly</id>
    52. <phase>package</phase>
    53. <goals>
    54. <goal>assembly</goal>
    55. </goals>
    56. </execution>
    57. </executions>
    58. </plugin>
    59. </plugins>
    60. </build>
    61.  
    62. </project>

    右键Maven-----》Update Project:

    右键Run As ----》Maven install

    查看target目录,可以发现多了XXXXX--jar-with-dependencies.jar

    由于该jar包把所有的依赖包也打包进去了,所以会变得非常大。

  • 相关阅读:
    高仿中国银行ATM系统
    第二次冲刺2
    第二轮冲刺1
    本日进度7
    本日进度6
    本日进度5
    本日进度4
    本日进度3
    本日进度2
    本日进度
  • 原文地址:https://www.cnblogs.com/yangcx666/p/8723907.html
Copyright © 2020-2023  润新知