【问题标题】:How to Exclude Certain Jar Dependencies while creating jar without using maven?如何在不使用 maven 创建 jar 时排除某些 Jar 依赖项?
【发布时间】:2015-11-03 17:03:39
【问题描述】:

我正在开发一个核心 Java 项目。我正在编写一个 Apache Storm 拓扑,需要在将拓扑绑定到 jar 时排除风暴 jar。有没有办法在不使用 maven 的情况下做到这一点?我知道使用 maven 我们可以使用<scope>provided</scope>,但我需要一个替代方法。

PS:我正在使用 Eclipse。

【问题讨论】:

  • 如果不使用 maven,你如何构建你的 jar?
  • 使用eclipse将其导出到可执行jar
  • 尝试导出到“常规”jar 文件(即“JAR 文件”而不是“可运行的 JAR 文件”)。
  • 作为常规 jar 导出,将不包含任何依赖项。如果你需要一些但不是所有的依赖,我猜 Eclipse 不支持这个。我真的建议使用 maven(或其他构建工具——也许是 ant)
  • 为什么不试试 maven 中的<exclusions> 标签?

标签: maven apache-storm


【解决方案1】:

我使用Gradle 为拓扑编译JAR 文件。它允许您在生成 JAR 文件时排除某些文件。

下面的示例显示了我在 build.gradle 文件中使用的设置

   apply plugin: 'java'
    apply plugin: 'eclipse'


    configurations {
        provided
        compile.extendsFrom provided
    }

    jar {
        dependsOn configurations.runtime

        from {
            (configurations.runtime - configurations.provided).collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }

        manifest {
            attributes 'Main-Class': 'com.example.myclass'
        }
    }

    dependencies {
        provided files('./lib/asm-4.0.jar')
        provided files('./lib/carbonite-1.4.0.jar')
        # ... The rest of the Storm jars found in the lib directory of your storm installation
    }

Gradle 期望的目录结构默认是

MyProjectName
    - build.gradle
    - src
       - main
            - java
                - [Your Java Files]
            - resources
                - resources
                    - [Mutlilang files / other resources]

当您在包含您的 build.gradle 文件的目录中运行 gradle build 时,从命令行应该会在 .\build\libs 下生成一个 JAR 文件

还有一个Gradle plugin for eclipse

【讨论】:

    【解决方案2】:

    如果您使用 Maven 而不是 Gradle,并且您来这里是为了在构建中排除 Storm 的 lib,我有解决方案和一个工作示例。

    documentation of Storm 告诉我们排除 Storm 依赖并提供 a link to a page of Maven,它解释了如何执行此操作,但缺少完整示例。事实证明,我们必须创建另一个 XML 文件作为程序集配置的描述符,而不是将配置直接放在 pom.xml 中,即使 Eclipse 不会抱怨将两个文件放在一起。

    方法如下:

    我们有 pom.xmlassembly 插件,指向另一个描述符 xml 文件:

    <plugin>                                                                                
        <artifactId>maven-assembly-plugin</artifactId>                                      
        <executions>                                                                        
            <execution>                                                                     
                <id>make-assembly</id> <!-- this is used for inheritance merges -->         
                <phase>package</phase> <!-- bind to the packaging phase -->                 
                <goals>                                                                     
                    <goal>single</goal>                                                     
                </goals>                                                                    
            </execution>                                                                    
        </executions>                                                                       
        <configuration>                                                                     
            <descriptors>                                                                   
                <descriptor>/src/main/resources/exclude-storm.xml</descriptor>              
            </descriptors>                                                                  
            <archive>                                                                       
                <manifest>                                                                  
                    <mainClass>path.to.main.class</mainClass> 
                </manifest>                                                                 
            </archive>                                                                      
            <descriptorRefs>                                                                
                <descriptorRef>jar-with-dependencies</descriptorRef>                        
            </descriptorRefs>                                                               
        </configuration>                                                                    
    </plugin>     
    

    注意部分:(应该是完整路径)

    <descriptors>                                                                   
        <descriptor>/src/main/resources/exclude-storm.xml</descriptor>              
    </descriptors>
    

    在这条路上:

    <?xml version="1.0" encoding="UTF-8"?>
    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
        <id>exclude-storm</id>
        <formats>
            <format>jar</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <dependencySets>
            <dependencySet>
                <outputDirectory>/</outputDirectory>
                <useProjectArtifact>false</useProjectArtifact>
                <unpack>true</unpack>
                <scope>compile</scope>   <!-- note here!!!! -->
                <excludes>
                    <exclude>org.apache.storm:storm-core:jar:1.1.1</exclude>
                </excludes>
            </dependencySet>
        </dependencySets>
        <fileSets>
            <fileSet>
                <outputDirectory>/</outputDirectory>
                <directory>${project.build.outputDirectory}</directory>
            </fileSet>
        </fileSets>
    </assembly>
    

    这里我们添加 Maven 文档页面的设置。

    最重要的是:排除格式:应该是:(ref)

    groupId:artifactId:type[:classifier]:version
    

    还有范围! runtime 是默认的,我把它改成compile 就可以了。

    最后,编译时使用:

    clean assembly:assembly
    

    并打开调试输出以在控制台中查看完整输出。如果你:

    • 构建成功
    • 搜索输出并没有找到类似的东西:[WARNING]The following patterns were never triggered in this artifact inclusion filter: o 'org.apache.storm:storm-core:jar:1.1.1'.
    • 罐子里没有default.yaml

    那你就知道你成功了。

    感谢另一个问答的启发:How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

    【讨论】:

      猜你喜欢
      • 2013-03-09
      • 2021-03-31
      • 1970-01-01
      • 2017-07-19
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      相关资源
      最近更新 更多