maven-shade-plugin error: Cannot find setter, adde

2019-05-11 01:19发布

问题:

I was trying to run the Hello World example from Spring Boot. When I run 'mvn package' on the module I get the following error:-

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.1:shade (default) on project spring.boot: Unable to parse configuration of mojo org.apache.maven.plugins:maven->shade->plugin:2.1:shade for parameter transformer: Cannot find setter, adder nor field in org.apache.maven.plugins.shade.resource.ManifestResourceTransformer for 'resource' -> [Help 1]

I haven't used any 'resource' attribute, but it seems to complain about this. Any idea what I am doing wrong here

Here is the pom for the module:-

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework</groupId>
    <artifactId>spring.boot</artifactId>
    <version>0.1.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.0.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>hello.SampleController</Main-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

回答1:

The first thing you could try is to not use the shade plugin (it's an inferior solution in general to the spring-boot plugin). The spring-boot plugin should be the one you find in all samples and guides in spring.io. With the starter-parent in use it's pretty trivial:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

The parent has a configuration for ManifestResourceTransformer already, so I assume that's the problem. If you remove the <executions/> and set a property start-class it should work.

If you need to use shade for some reason and you want to take control of the configuration, then don't use the starter-parent as a parent (maybe just use it for dependency management).



回答2:

In my case, I use Command Prompt (cmd.exe) to package the project. When maven packs the project, it will download all plugins, dependencies (Apache Maven Shade Plugin in this case). Cd to your project. Type:

mvn package

After packaging, it works. Hope it helps.