如何创建maven jar包,只包含依赖项

时间:2017-12-16 07:01:03

标签: java maven

我有一个Maven POM.xml文件,包括我的类依赖项。使用命令mvn clean compile assembly:single我可以创建一个包含所有依赖项的胖可执行jar。现在我想知道我是否可以创建一个不可执行的jar,包括POM.xml中的依赖项,并且不包含任何代码或类?

以下是POM.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Saprk</groupId>
    <artifactId>SparkPMUProcessing</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>SparkStreaming</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming_2.11</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.11</artifactId>
            <version>0.10.0.0</version>
        </dependency>
    </dependencies>
</project>

2 个答案:

答案 0 :(得分:0)

这也可以使用maven shade plugin完成。只需配置包含和排除所需文件的过滤器。Selecting Contents for Uber JAR

答案 1 :(得分:0)

一种解决方案是为依赖项创建单独的模块。您的根Code-behind看起来像这样

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class GroupListTapExample : ContentPage
{
    readonly ICommand tapCommand;
    List<GroupedItems> _listOfPeople;
    public List<GroupedItems> ListOfItems { get { return _listOfPeople; } set { _listOfPeople = value; base.OnPropertyChanged(); } }

    public GroupListTapExample()
    {
        InitializeComponent();

        tapCommand = new Command(OnTapped);

        var aList = new GroupedItems()
        {
            new Item() { Name = "Aa" },
            new Item() { Name = "Ab" }
        };
        aList.Heading = "A";

        var bList = new GroupedItems()
        {
            new Item() { Name = "Ba" },
            new Item() { Name = "Bb" }
        };
        bList.Heading = "B";

        var list = new List<GroupedItems>()
        {
            aList,
            bList
        };
        ListOfItems = list;

        BindingContext = this;
    }

    public ICommand TapCommand
    {
        get { return tapCommand; }
    }

    void OnTapped(object parameter)
    {
        var item = parameter as Item;
        DisplayAlert("Tapped", $"{item.Name}", "Cool");
    }
}

然后在依赖项中模块的pom定义所有yor依赖项并像你一样使用pom.xml

<packaging>pom</packaging>
<modules>
    <module>core</module>
    <module>dependencies</module>
</modules>

在主模块中引用依赖项模块作为依赖项