【问题标题】:Build Jar with dependencies including multiple main classes using Gradle in IntelliJ在 IntelliJ 中使用 Gradle 构建具有依赖项的 Jar,包括多个主类
【发布时间】:2021-03-03 13:44:17
【问题描述】:

我们目前正在使用 Maven 构建一个 Scala 应用程序。现在我们正在尝试将这个项目转换为 Gradle。我在这个项目中有 3/4 个主类,我想构建一个包含所有主类的依赖项的 jar,并通过调用任何类使用 spark-submit 执行这个 jar。

我是 Gradle 新手,面临 Gradle 问题。谁能帮帮我。

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>org-groupId</groupId>
    <artifactId>project-name</artifactId>
    <version>3.0-SNAPSHOT</version>

    <properties>
        <scala.version>2.11.12</scala.version>
        <scala.compat.version>2.11</scala.compat.version>
        <scala.test.version>3.0.4</scala.test.version>
        <project.type>application</project.type>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.42.Final</version>
        </dependency>
        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_${scala.compat.version}</artifactId>
            <version>3.0.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>maven-repo</id>
            <name>Maven central repository</name>
            <url>https://repo1.maven.org/maven2</url>
        </repository>
    </repositories>

    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.scalatest</groupId>
                <artifactId>scalatest-maven-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                    <junitxml>.</junitxml>
                </configuration>
                <executions>
                    <execution>
                        <id>test</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>4.3.0</version>
                <executions>
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>scala-test-compile</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>11</release>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.scalastyle</groupId>
                <artifactId>scalastyle-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <verbose>false</verbose>
                    <failOnViolation>true</failOnViolation>
                    <includeTestSourceDirectory>true</includeTestSourceDirectory>
                    <failOnWarning>false</failOnWarning>
                    <sourceDirectory>src/main/scala</sourceDirectory>
                    <testSourceDirectory>src/test/scala</testSourceDirectory>
                    <configLocation>${project.basedir}/src/main/resources/plugin/scalastyle_config.xml</configLocation>
                    <outputFile>${project.build.directory}/scalastyle-output.xml</outputFile>
                    <outputEncoding>UTF-8</outputEncoding>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

build.gradle 的内容:

apply plugin: 'java'
apply plugin: 'scala'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'maven-publish'

repositories {
    mavenLocal()
    mavenCentral()

    maven {
        url = uri('https://repo1.maven.org/maven2')
    }
}

dependencies {
    // Some internal dependencies 
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'io.netty:netty-all:4.1.42.Final'
    testImplementation "org.scalatest:scalatest_2.11:$scalaTestVersion"
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

gradle.properties 的内容:

# Description

# Versions
version = 4.0
scalaVersion=2.11.12
scalaMajorVersion=2.11
scalaTestVersion=3.0.4
sparkVersion=2.4.6

sourceCompatibility = '1.8'

我试过./gradlew clean assemble./gradlew clean build

他们都构建了一个常规的 jar,但没有构建依赖项 jar。

另外,我正在尝试在 IntelliJ 中执行此操作,因为 Jar 没有构建,但 IntelliJ 收到以下错误。

21:36:59: Executing task 'mainclass1.main()'...


FAILURE: Build failed with an exception.

* Where:
Initialization script '/private/var/folders/sq/npjk1mkn7lgfm57mf9g_3rrh0000gn/T/mainclass1_main__.gradle' line: 4

* What went wrong:
A problem occurred configuring root project 'project-name'.
> Could not create task ':mainclass1.main()'.
   > Unnecessarily replacing a task that does not exist is not supported.  Use create() or register() directly instead.  You attempted to replace a task named 'mainclass1.main()', but there is no existing task with that name.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.4.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 567ms
Cause: invalid type code: FA
21:37:00: Task execution finished 'mainclass1.main()'.

从 Maven pom.xml 转换到 Gradle 时,我没有在 Gradle 中包含和构建插件。我可以使用哪些插件在 Gradle 中构建 jar。

【问题讨论】:

    标签: scala maven apache-spark gradle intellij-idea


    【解决方案1】:

    看起来你想要一个胖罐子。这将包含允许您运行应用程序的所有依赖项。​​​ Gradle 没有直接的开箱即用解决方案,因为它的目标是让事情变得简单。

    您可以通过在 build.gradle 文件中设置以下内容来做到这一点:

    jar {
       ​from {
           ​configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
       ​}
    }
    
    

    或者,您可以通过在build.gradle 上设置以下内容来使用此plugin

    plugins {
      id 'com.github.johnrengelman.shadow' version '5.2.0' // check version compatibiltiy
    }
    

    以及运行任务gradlew shadowJar

    另一方面,我知道你有 3/4 的主要课程吗?我不知道 SPARK,所以也许它允许几个主类,但是在 SPARK 之外,你的清单文件必须设置哪个是主类。只可以有一个人。 这将允许您运行java -jar your-jar.jar。以下 SO 问题可能是相关的:Multiple runnable classes inside JAR, how to run them?

    使用 gradle 在 Manifest 上设置你的主类:

    jar {
       ​manifest.attributes["Main-Class"] = "<your-main-class-full-path>"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-27
      • 2021-03-12
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 2018-06-30
      • 1970-01-01
      相关资源
      最近更新 更多