java—将特定的依赖项打包到jar中

jv4diomz  于 2021-05-30  发布在  Hadoop
关注(0)|答案(2)|浏览(364)

我有一个项目有很多hadoop依赖项和org.json依赖项。我只想将org.json依赖项打包到jar中,因为打包hadoop依赖项会导致冲突。我使用的是packaging=jar,而使用胖jar不适合我的情况。有没有办法只打包org.json依赖项?my pom.xml dependencies部分:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.hadoop</groupId>
  4. <artifactId>hadoop-hdfs</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.hadoop</groupId>
  8. <artifactId>hadoop-auth</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.apache.hadoop</groupId>
  12. <artifactId>hadoop-common</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.apache.hadoop</groupId>
  16. <artifactId>hadoop-core</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>junit</groupId>
  20. <artifactId>junit</artifactId>
  21. <version>4.10</version>
  22. <scope>test</scope>
  23. </dependency>
  24. <!-- JSON Parser dependency -->
  25. <dependency>
  26. <groupId>org.json</groupId>
  27. <artifactId>json</artifactId>
  28. </dependency>
  29. <!-- Log4J dependency -->
  30. <dependency>
  31. <groupId>log4j</groupId>
  32. <artifactId>log4j</artifactId>
  33. </dependency>
  34. </dependencies>
mctunoxg

mctunoxg1#

回答我自己的问题,也许这就是托比ørn ravn andersen的意思是:对于所有的依赖项,我不想被打包并提供。例子:

  1. <dependency>
  2. <groupId>org.apache.hadoop</groupId>
  3. <artifactId>hadoop-core</artifactId>
  4. <version>2.0.0-mr1-cdh4.0.1</version>
  5. <scope>provided</scope>
  6. </dependency>

把它 Package 成一个肥jar:

  1. <!-- Maven Assembly Plugin -->
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-assembly-plugin</artifactId>
  5. <version>2.4.1</version>
  6. <configuration>
  7. <!-- get all project dependencies -->
  8. <descriptorRefs>
  9. <descriptorRef>jar-with-dependencies</descriptorRef>
  10. </descriptorRefs>
  11. <!-- MainClass in mainfest make a executable jar -->
  12. <archive>
  13. <manifest>
  14. <mainClass>com.myorg.MyAppMain</mainClass>
  15. </manifest>
  16. </archive>
  17. </configuration>
  18. <executions>
  19. <execution>
  20. <id>make-assembly</id>
  21. <!-- bind to the packaging phase -->
  22. <phase>package</phase>
  23. <goals>
  24. <goal>single</goal>
  25. </goals>
  26. </execution>
  27. </executions>
  28. </plugin>

编辑:如果运行时依赖项也被排除在类路径之外,那么项目就不能在我的ide上运行。

展开查看全部
xvw2m8pv

xvw2m8pv2#

你可以使用maven shade插件,详细说明什么去了什么不去了;如果不想,在使用shade时不必重命名任何内容。

相关问题