重新打包失败:使用Spring-Boot-Starter-Parent版本2.1.2创建库时找不到主类

时间:2019-02-11 18:10:31

标签: spring spring-boot spring-security

我在Spring Security中使用包含WebSecurityConfigurerAdapter的库创建了一个示例资源服务器。当我将库的pom中的父项spring-boot-starter-parent以及spring-boot-maven-plugin从2.0.8更新到2.1.2时,我得到了可怕的repackage failed: Unable to find main class spring-boot-starter-parent的2.0.8版本没有这个问题。

更新:执行mvn clean编译安装时会发生这种情况。可以在here中找到父版本为2.0.8的代码版本。只需将pom.xml spring starter parent从2.0.8更改为2.1.2。

我的图书馆pom如下。

<?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>com.example.utils.security</groupId>
  <artifactId>resource-config</artifactId>
  <version>1.0.1</version>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.1.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.1.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-config</artifactId>
      <version>5.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-oauth2-jose</artifactId>
      <version>5.1.3.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-oauth2-resource-server -->
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-oauth2-resource-server</artifactId>
      <version>5.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>5.1.3.RELEASE</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.1.2.RELEASE</version>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
            <configuration>
              <skip>true</skip>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

定义注释的类。


import com.example.utils.security.resource.autoconfig.ResourceServerConfig;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({ResourceServerConfig.class})
public @interface EnableExampleResourceServer {
}

WebSecurityConfigurerAdapter

package com.example.utils.security.resource.autoconfig;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
@Order(PriorityOrdered.HIGHEST_PRECEDENCE + 500)
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .anonymous().disable()
                .logout().disable()
                .formLogin().disable()
                .httpBasic().disable()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .anyRequest().authenticated()
                .and()
                .oauth2ResourceServer().jwt();
    }
}

3 个答案:

答案 0 :(得分:1)

发行说明描述了the repackage goal has an identifier now,以便更容易覆盖repackage执行。

上面的版本有点奇怪。如果您不希望重新打包,为什么要定义目标呢?只是不定义目标,并且不会进行重新打包,因为父级仅在显式定义插件时才提供默认执行。

如果出于其他原因需要定义它,则上面的链接说明了您应该做什么。目前,您正在重新定义repackage目标的第二次执行。

答案 1 :(得分:0)

可能有2种可能性-

  1. 项目中没有main()方法或

  2. 源目录的位置错误。您必须使用sourceSets指令来解决此问题。您的源目录应类似于src/main/java/your/package.

答案 2 :(得分:0)

我通过使用maven-compiler-plugin而不是spring-boot-maven-plugin解决了这个问题。仍然找出是什么使spring-boot-maven-plugin导致此问题。

Maven插件详细信息:

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>