我有一个多项目构建,我在一个子项目中设置了一个任务来构建一个胖罐.我创建的任务类似于食谱中描述的任务.
jar {
from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
manifest { attributes 'Main-Class': 'com.benmccann.gradle.test.WebServer' }
}
Run Code Online (Sandbox Code Playgroud)
运行它会导致以下错误:
原因:您无法更改未处于未解决状态的配置!
我不确定这个错误意味着什么.我还在Gradle JIRA上报告了这个问题,以防它出现问题.
Ben*_*ann 163
我在JIRA中针对Gradle 发布了一个解决方案:
// Include dependent libraries in archive.
mainClassName = "com.company.application.Main"
jar {
manifest {
attributes "Main-Class": "$mainClassName"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
Run Code Online (Sandbox Code Playgroud)
Fel*_*lix 62
如果您希望jar
任务正常运行并且有额外的fatJar
任务,请使用以下命令:
task fatJar(type: Jar) {
classifier = 'all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
Run Code Online (Sandbox Code Playgroud)
重要的是with jar
.没有它,这个项目的类不包括在内.
blo*_*ets 56
@felix的回答几乎把我带到了那里.我有两个问题:
以下设置解决了这个问题
jar {
manifest {
attributes(
'Main-Class': 'my.project.main',
)
}
}
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
Run Code Online (Sandbox Code Playgroud)
要将其添加到标准汇编或构建任务,请添加:
artifacts {
archives fatJar
}
Run Code Online (Sandbox Code Playgroud)
编辑:感谢@mjaggard:在最新版本的Gradle中,更改configurations.runtime
为configurations.runtimeClasspath
Lan*_*abs 24
由于现在不推荐使用编译来列出依赖项,并且所有依赖项都应该切换到实现,因此构建具有所有依赖项的 Jar 的解决方案应该使用此网站中的示例。
https://docs.gradle.org/current/userguide/working_with_files.html#sec:creating_uber_jar_example
具体来说这个命令:
configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it)
Run Code Online (Sandbox Code Playgroud)
这是完整的 gradle 部分: [1]:https://docs.gradle.org/current/userguide/working_with_files.html#sec:creating_uber_jar_example
task uberJar(type: Jar) {
archiveClassifier = 'uber'
from sourceSets.main.output
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
}}
Run Code Online (Sandbox Code Playgroud)
这对我来说很好.
我的主要课程:
package com.curso.online.gradle;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
public class Main {
public static void main(String[] args) {
Logger logger = Logger.getLogger(Main.class);
logger.debug("Starting demo");
String s = "Some Value";
if (!StringUtils.isEmpty(s)) {
System.out.println("Welcome ");
}
logger.debug("End of demo");
}
}
Run Code Online (Sandbox Code Playgroud)
它是我的文件build.gradle的内容:
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
compile 'org.apache.commons:commons-lang3:3.0'
compile 'log4j:log4j:1.2.16'
}
task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'com.curso.online.gradle.Main'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
Run Code Online (Sandbox Code Playgroud)
我在我的控制台中写下以下内容:
java -jar ProyectoEclipseTest-all.jar
Run Code Online (Sandbox Code Playgroud)
输出很棒:
log4j:WARN No appenders could be found for logger (com.curso.online.gradle.Main)
.
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more in
fo.
Welcome
Run Code Online (Sandbox Code Playgroud)
小智 8
@ben 的答案几乎对我有用,只是我的依赖项太大并且出现以下错误
Execution failed for task ':jar'.
> archive contains more than 65535 entries.
To build this archive, please enable the zip64 extension.
Run Code Online (Sandbox Code Playgroud)
要解决此问题,我必须使用以下代码
mainClassName = "com.company.application.Main"
jar {
manifest {
attributes "Main-Class": "$mainClassName"
}
zip64 = true
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
Run Code Online (Sandbox Code Playgroud)
根据 @blootsvoets 提出的解决方案,我这样编辑了我的 jar 目标:
jar {
manifest {
attributes('Main-Class': 'eu.tib.sre.Main')
}
// Include the classpath from the dependencies
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
// This help solve the issue with jar lunch
{
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
}
Run Code Online (Sandbox Code Playgroud)
要使用主可执行类生成胖JAR,避免签名JAR的问题,我建议使用gradle-one-jar插件.一个使用One-JAR项目的简单插件.
使用方便:
apply plugin: 'gradle-one-jar'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.rholder:gradle-one-jar:1.0.4'
}
}
task myjar(type: OneJar) {
mainClass = 'com.benmccann.gradle.test.WebServer'
}
Run Code Online (Sandbox Code Playgroud)
小智 5
简单的溶液
jar {
manifest {
attributes 'Main-Class': 'cova2.Main'
}
doFirst {
from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
124619 次 |
最近记录: |