如何在不使用 maven 的情况下创建 jar 时排除某些 Jar 依赖项? [英] How to Exclude Certain Jar Dependencies while creating jar without using maven?

查看:32
本文介绍了如何在不使用 maven 的情况下创建 jar 时排除某些 Jar 依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a core Java project. I am writing an Apache Storm topology and need to exclude storm jars while binding the topology into jar. Is there any way to do this without using maven? I know with maven we can use <scope>provided</scope> but I need an alternative to this.

PS:我正在使用 Eclipse.

PS: I am using Eclipse.

推荐答案

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

If you are using Maven instead of Gradle, and you come here for excluding lib of Storm in building, I have the solution and a working example.

Storm 文档 告诉我们排除 Storm 依赖并提供 指向 Maven 页面的链接,其中解释了如何执行此操作,但缺少完整示例.事实证明,我们必须创建另一个XML文件作为程序集配置的描述符,而不是直接把配置放在pom.xml中,即使Eclipse没有抱怨把这两个文件放在一起.

The documentation of Storm tells us to exclude the Storm dependency and provides a link to a page of Maven, which explains how to do it but it lacks a full example. It turns out that we have to create another XML file as the descriptor of assembly configuration, instead of putting the configuration directly in the pom.xml, even when Eclipse does not complain about putting the two files together.

方法如下:

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

We have our pom.xml with assembly plugins, pointing to another descriptor xml file:

<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>     

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

Note the part of: (should be complete path)

<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 doc页面的设置.

Here we add the settings of Maven doc page.

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

The most important thing: format of exclusion: should be: (ref)

groupId:artifactId:type[:classifier]:version

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

And the scope! runtime is default, I changed it to compile and it works.

最后编译时使用:

clean assembly:assembly

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

And turn on debug output to see full output in console. If you:

  • 构建成功
  • 搜索输出并没有找到类似的内容:[警告]以下模式从未在此工件包含过滤器中触发:o 'org.apache.storm:storm-core:jar:1.1.1'.
  • jar 不包含 default.yaml

然后你就知道你成功了.

Then you know you have succeeded.

感谢另一个问题和答案的启发:如何从 Maven 程序集插件中排除依赖项:jar-with-dependencies?

Thanks for the inspiration of another question and answers: How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

这篇关于如何在不使用 maven 的情况下创建 jar 时排除某些 Jar 依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆