Gradle shadow插件失败:解析插件时出错

时间:2016-08-24 08:50:45

标签: java gradle build.gradle gradle-shadow-plugin

我正在将JAVA maven项目迁移到gradle。我在maven中添加了一个Shade插件,但当我试图在gradle中复制它时,它当前失败了。它在清洁时失败,即当我发出以下命令时,

gradle clean

请在下面找到pom.xml文件和相应的build.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>com.myproj.integration</groupId>
<artifactId>Generic-Integration</artifactId>
<name>Generic-Integration</name>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <finalName>myproj-integration-${project.version}</finalName>

                <shadedArtifactAttached>true</shadedArtifactAttached>
                <createDependencyReducedPom>true</createDependencyReducedPom>

                <relocations>
                    <relocation>
                        <pattern>jersey.repackaged</pattern>
                        <shadedPattern>myproj.repackaged</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>javassist</pattern>
                        <shadedPattern>myproj.repackaged.javassist</shadedPattern>
                    </relocation>

                    <relocation>
                        <pattern>org.aopalliance</pattern>
                        <shadedPattern>myproj.repackaged.org.aopalliance</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.apache.logging</pattern>
                        <shadedPattern>myproj.repackaged.org.apache.logging</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.codehaus.jackson</pattern>
                        <shadedPattern>myproj.repackaged.org.codehaus.jackson</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.glassfish.hk2</pattern>
                        <shadedPattern>myproj.repackaged.org.glassfish.hk2</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.glassfish.jersey</pattern>
                        <shadedPattern>myproj.repackaged.org.glassfish.jersey</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.json</pattern>
                        <shadedPattern>myproj.repackaged.org.json</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.jvnet</pattern>
                        <shadedPattern>myproj.repackaged.org.jvnet</shadedPattern>
                    </relocation>

                    <relocation>
                        <pattern>javax.annotation</pattern>
                        <shadedPattern>myproj.repackaged.javax.annotation</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>javax.inject</pattern>
                        <shadedPattern>myproj.repackaged.javax.inject</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>javax.ws</pattern>
                        <shadedPattern>myproj.repackaged.javax.ws</shadedPattern>
                    </relocation>
                </relocations>
                <transformers>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.myproj.integration.ServiceExecuter</mainClass>
                    </transformer>
                </transformers>

                <artifactSet>
                    <includes>
                        <include>com.myproj:Util</include>
                        <include>org.glassfish.hk2:hk2-api</include>
                        <include>org.glassfish.hk2:hk2-utils</include>
                        <include>org.glassfish.hk2:hk2-locator</include>
                        <include>org.glassfish.hk2:osgi-resource-locator</include>
                        <include>org.glassfish.hk2.external:aopalliance-repackaged</include>
                        <include>org.codehaus.jackson:jackson-core-asl</include>
                        <include>org.codehaus.jackson:jackson-mapper-asl</include>
                        <include>org.codehaus.jackson:jackson-jaxrs</include>
                        <include>org.codehaus.jackson:jackson-xc</include>
                        <include>org.javassist:javassist</include>
                        <include>javax.annotation:javax.annotation-api</include>
                        <include>org.glassfish.hk2.external:javax.inject</include>
                        <include>javax.ws.rs:javax.ws.rs-api</include>
                        <include>org.glassfish.jersey.core:jersey-client</include>
                        <include>org.glassfish.jersey.core:jersey-common</include>
                        <include>org.glassfish.jersey.bundles.repackaged:jersey-guava</include>
                        <include>org.glassfish.jersey.media:jersey-media-json-jackson</include>
                        <include>org.json:json</include>
                        <include>com.myproj:JAXRS</include>
                    </includes>
                </artifactSet>

            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.myproj</groupId>
        <artifactId>JAXRS</artifactId>
        <version>1.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>8.1.16.v20140903</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.myproj</groupId>
        <artifactId>Server</artifactId>
        <version>1.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.8</version>
        <scope>test</scope>
    </dependency>
</dependencies>

的build.gradle

    plugins {
        id 'com.github.johnrengelman.shadow' version '1.2.3'
    }

    shadowJar {
        artifactSet {
            include 'com.myproj:Util'
            include 'org.glassfish.hk2:hk2-api'
            include 'org.glassfish.hk2:hk2-utils'
            include 'org.glassfish.hk2:hk2-locator'
            include 'org.glassfish.hk2:osgi-resource-locator'
            include 'org.glassfish.hk2.external:aopalliance-repackaged'
            include 'org.codehaus.jackson:jackson-core-asl'
            include 'org.codehaus.jackson:jackson-mapper-asl'
            include 'org.codehaus.jackson:jackson-jaxrs'
            include 'org.codehaus.jackson:jackson-xc'
            include 'org.javassist:javassist'
            include 'javax.annotation:javax.annotation-api'
            include 'org.glassfish.hk2.external:javax.inject'
            include 'javax.ws.rs:javax.ws.rs-api'
            include 'org.glassfish.jersey.core:jersey-client'
            include 'org.glassfish.jersey.core:jersey-common'
            include 'org.glassfish.jersey.bundles.repackaged:jersey-guava'
            include 'org.glassfish.jersey.media:jersey-media-json-jackson'
            include 'org.json:json'
            include 'com.myproj:JAXRS'
        }
        relocation {
            pattern = 'jersey.repackaged'
            shadedPattern = 'myproj.repackaged'
        }
        relocation {
            pattern = 'javassist'
            shadedPattern = 'myproj.repackaged.javassist'
        }
        relocation {
            pattern = 'org.aopalliance'
            shadedPattern = 'myproj.repackaged.org.aopalliance'
        }
        relocation {
            pattern = 'org.apache.logging'
            shadedPattern = 'myproj.repackaged.org.apache.logging'
        }
        relocation {
            pattern = 'org.codehaus.jackson'
            shadedPattern = 'myproj.repackaged.org.codehaus.jackson'
        }
        relocation {
            pattern = 'org.glassfish.hk2'
            shadedPattern = 'myproj.repackaged.org.glassfish.hk2'
        }
        relocation {
            pattern = 'org.glassfish.jersey'
            shadedPattern = 'myproj.repackaged.org.glassfish.jersey'
        }
        relocation {
            pattern = 'org.json'
            shadedPattern = 'myproj.repackaged.org.json'
        }
        relocation {
            pattern = 'org.jvnet'
            shadedPattern = 'myproj.repackaged.org.jvnet'
        }
        relocation {
            pattern = 'javax.annotation'
            shadedPattern = 'myproj.repackaged.javax.annotation'
        }
        relocation {
            pattern = 'javax.inject'
            shadedPattern = 'myproj.repackaged.javax.inject'
        }
        relocation {
            pattern = 'javax.ws'
            shadedPattern = 'myproj.repackaged.javax.ws'
        }
    }

    dependencies {
      compile project(':JAXRS')
      compile project(':Server')
      testCompile group: 'org.testng', name: 'testng', version:'6.8.8'
      compile(group: 'org.eclipse.jetty', name: 'jetty-servlet', version:'8.1.16.v20140903') {
           /* This dependency was originally in the Maven provided scope, but the project was not of type war.
           This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
           Please review and delete this closure when resolved. */
      }
    }

    task genericIntegrationJar(type: Jar) {
        baseName = 'myproj-integration-1.2'
        from { configurations.external.collect { it.isDirectory() ? it : zipTree(it) } }
        with jar
    }

    test.useTestNG()

当我干净的时候,我得到了以下异常,

        FAILURE: Build failed with an exception.

        * Where:
        Build file '/Users/mac8/POS/MobilePosSolution/Generic-Integration/build.gradle' line: 2

        * What went wrong:
        Error resolving plugin [id: 'com.github.johnrengelman.shadow', version: '1.2.3']
        > The response from https://plugins.gradle.org/api/gradle/2.14.1/plugin/use/com.github.johnrengelman.shadow/1.2.3 was not a valid response from a Gradle Plugin Resolution Service: content type is 'text/html;charset=iso-8859-1', expected 'application/json' (status code: 404)

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

        BUILD FAILED

        Total time: 4.903 secs

请告诉我,如果我需要在 build.gradle 文件中更改某些内容,以便复制阴影插件。

2 个答案:

答案 0 :(得分:0)

我只是试着点击它,看起来它工作正常:

curl -v https://plugins.gradle.org/api/gradle/2.14.1/plugin/use/com.github.johnrengelman.shadow/1.2.3
*   Trying 104.25.173.23...
* Connected to plugins.gradle.org (104.25.173.23) port 443 (#0)
* found 173 certificates in /etc/ssl/certs/ca-certificates.crt
* found 704 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
* SSL connection using TLS1.2 / ECDHE_ECDSA_AES_128_GCM_SHA256
*    server certificate verification OK
*    server certificate status verification SKIPPED
*    common name: ssl277771.cloudflaressl.com (matched)
*    server certificate expiration date OK
*    server certificate activation date OK
*    certificate public key: EC
*    certificate version: #3
*    subject: OU=Domain Control Validated,OU=PositiveSSL Multi-Domain,CN=ssl277771.cloudflaressl.com
*    start date: Sat, 06 Aug 2016 00:00:00 GMT
*    expire date: Sun, 05 Feb 2017 23:59:59 GMT
*    issuer: C=GB,ST=Greater Manchester,L=Salford,O=COMODO CA Limited,CN=COMODO ECC Domain Validation Secure Server CA 2
*    compression: NULL
* ALPN, server accepted to use http/1.1
> GET /api/gradle/2.14.1/plugin/use/com.github.johnrengelman.shadow/1.2.3 HTTP/1.1
> Host: plugins.gradle.org
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Wed, 24 Aug 2016 09:24:42 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
< Set-Cookie: __cfduid=d0c64c916428b3abd24c29d0578251d2d1472030681; expires=Thu, 24-Aug-17 09:24:41 GMT; path=/; domain=.gradle.org; HttpOnly
< Via: 1.1 vegur
< Server: cloudflare-nginx
< CF-RAY: 2d75c0b211512378-FRA
< 
{
  "id" : "com.github.johnrengelman.shadow",
  "version" : "1.2.3",
  "implementation" : {
    "gav" : "com.github.jengelman.gradle.plugins:shadow:1.2.3",
    "repo" : "https://plugins.gradle.org/m2"
  },
  "implementationType" : "M2_JAR",
  "legacy" : true
* Connection #0 to host plugins.gradle.org left intact

试试看看会发生什么。

---编辑

只需将其添加到示例项目中即可正常下载文件。我还使用jcenter()存储库来存在依赖关系。

Download https://jcenter.bintray.com/com/github/jengelman/gradle/plugins/shadow/1.2.3/shadow-1.2.3.jar

将其添加到 build.gradle 文件并尝试

buildscript {
    repositories {
        jcenter()
    }
}

答案 1 :(得分:0)

这对我有用: -

jar {
    baseName = 'Project name'
    // adds runtime dependencies to jar package
    from({configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }}) {
        // remove all signature files
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
        exclude "META-INF/LICENSE"
        exclude "META-INF/license"
        exclude "license/*"
    }
    manifest {
        attributes 'Main-Class': 'Main class'
    } }