我正在将一个Maven项目迁移到STS中。
我的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>
我已经使用自动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'
}
对于这个构建是成功的,但是当我作为spring boot app运行时
Error: Could not find or load main class com.myPack.mindsphere.devproxy.DevProxyApp
我已经尝试过刷新,重建,更新gradle项目等。我还指定了如下的主类名称:
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'com.myPack.mindsphere.devproxy.DevProxyApp'
)
}
}
可能是什么问题:
最佳答案
您是否尝试过使用:
./gradlew bootRepackage
或./gradlew bootJar
生成可执行的jar ./gradlew bootRun
运行按照描述
https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/gradle-plugin/reference/html/
如果您使用的是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. Both tasks extend their equivalent standard Gradle jar or war task, giving you access to all of the usual configuration options and behaviour.
您应用的运行配置:
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'
}
gradle构建
gradlew引导运行
关于java - 从Main迁移到Gradle时未发现主类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50577412/