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

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

问题描述

我正在研究一个核心Java项目.我正在编写一个Apache Storm拓扑,并且需要在将拓扑绑定到jar时排除风暴jar.没有使用Maven的方法,有什么办法吗?我知道使用maven可以使用<scope>provided</scope>,但是我需要一个替代方法.

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库,我将提供解决方案和可行的示例.

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依赖性,并提供

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

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.

最后,在编译时,请使用:

At last, when you compile, use:

clean assembly:assembly

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

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

  • 构建成功
  • 搜索输出,未找到类似以下内容的内容:[WARNING]The following patterns were never triggered in this artifact inclusion filter: o 'org.apache.storm:storm-core:jar:1.1.1'.
  • 罐子中没有default.yaml
  • have a successful build
  • search the output and haven't found anything like: [WARNING]The following patterns were never triggered in this artifact inclusion filter: o 'org.apache.storm:storm-core:jar:1.1.1'.
  • the jar contains no default.yaml

那你就知道自己成功了.

Then you know you have succeeded.

感谢另一个问题和答案的启发:

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天全站免登陆