简体   繁体   English

从Main迁移到Gradle时找不到主类错误

[英]Main Class not found error migrating from maven to gradle

I am migrating a maven project to gradle in STS. 我正在将一个Maven项目迁移到STS中。

My pom.xml is : 我的pom.xml是:

    <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>com.myPack</groupId>
    <artifactId>sdk-devproxy</artifactId>
    <version>0.0.1-RELEASE</version>
    <packaging>jar</packaging>

    <name>sdk-devproxy</name>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>1.5.10.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>Edgware.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.auth0/java-jwt -->
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.3.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>sdk-devproxy</finalName>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.myPack.devproxy.DevProxyApp</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>

        </plugins>
    </build>
</project>

I have used automatic maven to gradle conversion command "gardle init" and I got the below build.gradle. 我已经使用自动Maven来转换命令“ gardle init”,并且得到了以下build.gradle。

    apply plugin: 'java'
apply plugin: 'maven'

group = 'com.myPack'
version = '0.0.1-RELEASE'

description = """sdk-devproxy"""

sourceCompatibility = 1.8
targetCompatibility = 1.8



repositories {

     maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-oauth2', version:'1.2.2.RELEASE'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zuul', version:'1.4.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.5.10.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j', version:'1.3.8.RELEASE'
    compile group: 'com.auth0', name: 'java-jwt', version:'3.3.0'
}

For this build is successful but while I run as spring boot app its giving 对于这个构建是成功的,但是当我作为spring boot app运行时

    Error: Could not find or load main class com.myPack.mindsphere.devproxy.DevProxyApp

I have tried refreshing,rebuilding, update gradle project etc.. i have also specified main class name like below : 我已经尝试过刷新,重建,更新gradle项目等。我还指定了如下的主类名称:

    jar {
  manifest {
    attributes(
      'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
      'Main-Class': 'com.myPack.mindsphere.devproxy.DevProxyApp'
    )
  }
}

What might be the issue: 可能是什么问题:

  • plugin or packaging issue? 插件或包装问题?
  • jar compatibility issue ? jar兼容性问题?
  • or something else ? 或者是其他东西 ?

Did you try using: 您是否尝试过使用:

  • ./gradlew bootRepackage or ./gradlew bootJar to generate the executable jar ./gradlew bootRepackage./gradlew bootJar生成可执行jar
  • ./gradlew bootRun to run ./gradlew bootRun运行

described as per 按照描述

https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/gradle-plugin/reference/html/ https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/gradle-plugin/reference/html/

If you are on gradle version Gradle 3.4 or later which uses bootJar task in place of bootRepackage as per latest documentation https://spring.io/blog/2017/04/05/spring-boot-s-new-gradle-plugin 如果您使用的是Gradle版本Gradle 3.4或更高版本,则根据最新文档https://spring.io/blog/2017/04/05/spring-boot-s-new-gradle-plugin使用bootJar任务代替bootRepackage

The bootRepackage task has been replaced with bootJar and bootWar tasks for building executable jars and wars respectively. bootRepackage任务已替换为bootJar和bootWar任务,分别用于构建可执行的jar和wars。 Both tasks extend their equivalent standard Gradle jar or war task, giving you access to all of the usual configuration options and behaviour. 两项任务都扩展了其等效的标准Gradle jar或war任务,使您可以访问所有常规配置选项和行为。

The running configuration for your app: 您应用的运行配置:

  1. Update your build file with below: 使用以下更新您的构建文件:

build.gradle build.gradle

apply plugin: 'java'
apply plugin: 'maven'
//apply plugin: 'io.spring.dependency-management'

group = 'com.myPack'
version = '0.0.1-RELEASE'

description = """sdk-devproxy"""

sourceCompatibility = 1.8
targetCompatibility = 1.8

buildscript {
    ext {
        springBootVersion = '2.0.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath(
          "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'org.springframework.boot'

repositories {

     maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-oauth2', version:'1.2.2.RELEASE'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zuul', version:'1.4.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.5.10.RELEASE'
    //compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j', version:'1.3.8.RELEASE'
    compile group: 'com.auth0', name: 'java-jwt', version:'3.3.0'
}

bootJar {
    mainClassName = 'com.myPack.mindsphere.devproy.DevProxyApp'
}
  1. Build the jar using below. 使用以下内容构建罐子。 This will generate sdk-devproxy-0.0.1-RELEASE.jar in build/libs directory of your project. 这将在项目的build / libs目录中生成sdk-devproxy-0.0.1-RELEASE.jar。

    gradle build Gradle构建

  2. Run the application using: 使用以下命令运行应用程序:

    gradlew bootRun gradlew bootRun

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM