Is There a Way to Replication Maven attachClasses

2019-08-05 10:53发布

问题:

Using Maven we are currently separating out classes from a WAR file using:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>${maven-war-plugin.version}</version>
    <configuration>
        <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
        <attachClasses>true</attachClasses>
        <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
    </configuration>
</plugin>

Is it possible to replicate this functionality in Gradle? The goal is to publish just the classes in a .jar alongside the primary war artifact:

ghc-templates-2.29-javadoc.jar       14-Jan-2014 15:19  112.43 KB
ghc-templates-2.29-sources.jar       14-Jan-2014 15:19  12.81 KB
ghc-templates-2.29.war               14-Jan-2014 15:19  6.05 MB
ghc-templates-2.29-classes.jar       14-Jan-2014 15:19  23.95 KB

回答1:

Yes, just run jar task. Since the War plugin applies the Java plugin you get all the regular tasks you would as if it were a normal java project. However, that task is not included in the dependency graph by default when running assemble (see the grade docs). To fix this simply add:

assemble {
    dependsOn jar
}

Also, as per your example above, if you want to add a custom classifier to the jar you can do so:

jar {
    classifier = 'classes'
}


标签: gradle