I'm building a fat jar with the code below. However, I have multiple property files with the same name in different jars, which are collected into the fat jar.
I guess the solution is to concatenate/merge the files with the same name into one file, but how do I do it? I found this question How do I concatenate multiple files in Gradle?.
How can I access and merge the property files (let's name them myproperties.properties) in the it objects?
task fatJar(type: Jar) {
def mainClass = "myclass"
def jarName = "myjarname"
zip64=true
manifest {
attributes(
'Main-Class': mainClass,
'Class-Path': configurations.compile.collect { it.getName() }.join(' ')
)
}
baseName = project.name + '-' + jarName + '-all'
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
{
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
with jar
}
Solution:
I marked the given answer as the solution although I haven't tried it. Instead we solved the problem by creating multiple jars and adding those to the classpath.
Possibly you could write a custom MergeCopy task. Eg
public interface FileMerger implements Serializable {
public void merge(String path, List<File> files, OutputStream out) throws IOException;
}
public class MergeCopy extends DefaultTask {
private File outputDir;
private List<FileTree> fileTrees = []
#TaskInput
FileMerger merger
#OutputDirectory
File getOutputDir() {
return outputDir
}
#InputFiles
List<FileTree> getFileTrees() {
return fileTrees
}
void from(FileTree fileTree) {
fileTrees.add(fileTree)
}
void into(Object into) {
outputDir = project.file(into)
}
#TaskAction
void copyAndMerge() {
Map<String, List<File>> fileMap = [:]
FileTree allTree = project.files().asFileTree
fileTrees.each { FileTree fileTree ->
allTree = allTree.plus(fileTree)
fileTree.visit { FileVisitDetails fvd ->
String path = fvd.path.path
List<File> matches = fileMap[path] ?: []
matches << fvd.file
fileMap[path] = matches
}
}
Set<String> dupPaths = [] as Set
Set<String> nonDupPaths = [] as Set
fileMap.each { String path, List<File> matches ->
if (matches.size() > 1) {
dupPaths << path
} else {
nonDupPaths << path
}
}
FileTree nonDups = allTree.matching {
exclude dupPaths
}
project.copy {
from nonDups
into outputDir
}
for (String dupPath : dupPaths) {
List<File> matches = fileMap[dupPath]
File outFile = new File(outputDir, dupPath)
outFile.parentFile.mkdirs()
try (OutputStream out = new FileOutputStream(outFile)) {
merger.merge(dupPath, matches, out)
out.flush()
}
}
}
}
You could then do
task mergeCopy(type: MergeCopy) {
configurations.compile.files.each {
from it.directory ? fileTree(it) : zipTree(it)
}
into "$buildDir/mergeCopy"
merger = { String path, List<File> files, OutputStream out ->
// TODO: implement
}
}
task fatJar(type: Jar) {
dependsOn mergeCopy
from mergeCopy.outputDir
...
}
Related
Below is the build.gradle file:
plugins({
id('application')
id 'java'
id('com.github.johnrengelman.shadow').version('4.0.1')
})
allprojects(
{
apply(plugin: 'application')
apply(plugin: 'java')
apply(plugin: 'com.github.johnrengelman.shadow')
repositories({
mavenCentral()
})
ext({
vertxVersion = '3.7.0'
commitTimestamp = {
return "git log -1 --pretty=format:%cd --date=format:%Y%m%d%H%M%S".execute().text.trim()
}
commitId = {
return "git rev-parse --short HEAD".execute().text.trim()
}
buildId = {
if (System.getenv("BUILD_ID") != null) return ".${System.getenv("BUILD_ID")}"
else return ""
}
})
group = 'com.pluralsight.docker-production-aws'
version = System.getenv("APP_VERSION") ?: "${commitTimestamp()}.${commitId()}${buildId()}"
sourceCompatibility = '1.8'
mainClassName = 'io.vertx.core.Launcher'
dependencies({
compile("io.vertx:vertx-core:$vertxVersion")
compile("io.vertx:vertx-hazelcast:$vertxVersion")
compile("io.vertx:vertx-service-discovery:$vertxVersion")
compile("io.vertx:vertx-dropwizard-metrics:$vertxVersion")
compile("com.typesafe:config:1.3.0")
compile("com.hazelcast:hazelcast-cloud:3.6.5")
testCompile("io.vertx:vertx-unit:$vertxVersion")
testCompile("junit:junit:4.12")
testCompile("org.assertj:assertj-core:3.5.2")
testCompile("com.jayway.awaitility:awaitility:1.7.0")
})
task(copyDeps(type: Copy), {
from (configurations.runtime + configurations.testRuntime).exclude('*')
into('/tmp')
}
)
test(
{
testLogging(
{
events("passed", "skipped", "failed")
}
)
reports(
{
junitXml.enabled = true
junitXml.destination = file("${rootProject.projectDir}/build/test-results/junit")
html.enabled = false
}
)
}
)
}
)
task(testReport(type: TestReport), {
destinationDir = file("${rootProject.projectDir}/build/test-results/html")
reportOn(subprojects*.test)
}
)
test(
{
dependsOn(testReport)
}
)
configure(
(subprojects - project(':microtrader-common')),
{
shadowJar(
{
destinationDir = file("${rootProject.projectDir}/build/jars")
classifier = 'fat'
mergeServiceFiles(
{
include('META-INF/services/io.vertx.core.spi.VerticleFactory')
}
)
}
)
}
)
task(
wrapper(type: Wrapper),
{
gradleVersion = '4.10.2'
}
)
that gives below error on /gradlew clean test shadowJar:
> Could not find method copyDeps() for arguments
for problem code snippet:
task(copyDeps(type: Copy), {
from (configurations.runtime + configurations.testRuntime).exclude('*')
into('/tmp')
}
task(testReport(type: TestReport), {
destinationDir = file("${rootProject.projectDir}/build/test-results/html")
reportOn(subprojects*.test)
}
)
task(
wrapper(type: Wrapper),
{
gradleVersion = '4.10.2'
}
)
./gradlew Command works with below code snippet syntax without paranthesis:
task copyDeps(type: Copy) {
from (configurations.runtime + configurations.testRuntime) exclude '*'
into '/tmp'
}
task testReport(type: TestReport) {
destinationDir = file("${rootProject.projectDir}/build/test-results/html")
reportOn subprojects*.test
}
task wrapper(type: Wrapper) {
gradleVersion = '4.10.2'
}
Does build.gradle have syntax issue? using paranthesis...We prefer using paranthesis
https://docs.gradle.org/current/userguide/more_about_tasks.html shows examples of how to define custom tasks.
Here is how you can define your tasks the verbose way.
tasks.create('copyDeps', Copy, {
from(file('srcDir'))
into(buildDir)
})
The tasks are created using the TaskContainer which offers several overloads for the create method. Here is a subset:
create(String name)
create(String name, Closure configureClosure)
create(String name, Class<T> type, Action<? super T> configuration) <-- this is the one used above
create(Map<String,?> options, Closure configureClosure)
So I've made a small desktop application in Eclipse (gradle project) based on libGDX. Runs perfectly in Eclipse. When I export as a "runnable JAR file" (with Package required libraries into generated JAR checked) I get a warning: "Fat Jar Export: Could not find class-path entry for 'D:Project TI Helper/core/bin/default/".
Is something missing in the manifest at "Class-Path: ." ?
I have no idea what this is about. But the JAR is certainly not runnable. So I try the command prompt and do "gradle desktop:dist --stacktrace". Then the JAR file seems to be produced without any errors or warnings. So I go to .../desktop/build/libs/ and try to run it with "java -jar desktop-1.0.jar", the texture packer starts packing but fails in the end with this message in the console.
The generated atlas-file IS in the specified location. The textures WERE packed. Why on earth is it not loading the stuff?? Btw, I'm using Java version 1.8.0_241 for both JDK and JRE.
EDIT
So it fails in the Assets class at "TextureAtlas atlas = assets2d.get(Cn.TEXTURES);". Going deeper into the AssetManager
public synchronized <T> T get (String fileName) {
Class<T> type = assetTypes.get(fileName);
if (type == null) throw new GdxRuntimeException("Asset not loaded: " + fileName);
...
So it seems the string provided is not pointing to my atlas-file. Cn.TEXTURES is defined as "../desktop/assets/atlas/textures.atlas". That raises the question: How DO you write the path?
DesktopLauncher.java
public class DesktopLauncher
{
public static boolean rebuildAtlas = true;
public static boolean drawDebugOutline = true;
public static void main (String[] arg)
{
// Build Texture Atlases
if (rebuildAtlas) {
Settings settings = new Settings();
settings.maxWidth = 2048;
settings.maxHeight = 2048;
settings.pot = false;
settings.combineSubdirectories = true;
// Pack images in "textures" folder
TexturePacker.process("assets/textures", "assets/atlas", "textures.atlas");
}
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "TI Helper";
cfg.useGL30 = false;
cfg.width = Cn.RESOLUTION_WIDTH;
cfg.height = Cn.RESOLUTION_HEIGHT;
cfg.fullscreen = true;
new LwjglApplication(new TIHelper(), cfg);
}
}
Assets.java
public class Assets implements Disposable, AssetErrorListener
{
public static final String TAG = Assets.class.getName();
public static final Assets instance = new Assets();
// Asset managers
public AssetManager assets2d;
public AssetDeco assetDeco;
public AssetFonts fonts;
public AssetMisc assetMisc;
// General fonts
public static Font not16, not20, not24, dig16;
// Singelton: prevent installation from other classes
private Assets() {}
public void init ()
{
// Init 2D graphics manager
init2DAssetManager();
TextureAtlas atlas = assets2d.get(Cn.TEXTURES);
// Cn.TEXTURES == String TEXTURES = "../desktop/assets/atlas/textures.atlas";
// Create game resource objects
fonts = new AssetFonts();
assetDeco = new AssetDeco(atlas);
assetMisc = new AssetMisc(atlas);
// Create fonts
not16 = new Font(Assets.instance.fonts.notalot_16);
not20 = new Font(Assets.instance.fonts.notalot_20);
not24 = new Font(Assets.instance.fonts.notalot_24);
dig16 = new Font(Assets.instance.fonts.digits_16);
}
private void init2DAssetManager()
{
// Create the manager
assets2d = new AssetManager();
// Set asset manager error handler
assets2d.setErrorListener(this);
// Load texture atlas
assets2d.load(Cn.TEXTURES, TextureAtlas.class);
// Start loading assets and wait until finished
assets2d.finishLoading();
// Prompt output
Gdx.app.debug(TAG, "# of assets loaded: " + assets2d.getAssetNames().size);
for (String a : assets2d.getAssetNames())
Gdx.app.debug(TAG, "asset: " + a);
}
#Override
public void dispose ()
{
assets2d.dispose();
}
public void error (String filename, Class<?> type, Throwable throwable) {
Gdx.app.error(TAG, "Couldn't load asset '" + filename + "'", (Exception)throwable);
}
...
Desktop build.gradle
apply plugin: "java"
sourceCompatibility = 1.8
sourceSets.main.java.srcDirs = [ "src/" ]
sourceSets.main.resources.srcDirs = ["../desktop/assets"]
project.ext.mainClassName = "com.ti.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../desktop/assets")
task run(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
}
task debug(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
debug = true
}
task dist(type: Jar) {
manifest {
attributes 'Main-Class': project.mainClassName
}
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
}
dist.dependsOn classes
eclipse.project.name = appName + "-desktop"
Workspace build.gradle
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
jcenter()
google()
}
dependencies {
}
}
allprojects {
apply plugin: "eclipse"
version = '1.0'
ext {
appName = "ti-helper"
gdxVersion = '1.9.10'
roboVMVersion = '2.3.8'
box2DLightsVersion = '1.4'
ashleyVersion = '1.7.0'
aiVersion = '1.8.0'
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
google()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":desktop") {
apply plugin: "java-library"
dependencies {
implementation project(":core")
api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
api "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
api "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
api "com.badlogicgames.gdx:gdx-controllers-desktop:$gdxVersion"
api "com.badlogicgames.gdx:gdx-controllers-platform:$gdxVersion:natives-desktop"
}
}
project(":core") {
apply plugin: "java-library"
dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
api "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
api "net.dermetfan.libgdx-utils:libgdx-utils:0.13.4"
api "net.dermetfan.libgdx-utils:libgdx-utils-box2d:0.13.4"
}
}
If someone could help me out it would much appreciated. This is my first project in libGDX and I'm very stuck here, and I'm all out of ideas on how to find any clues to what might be wrong.
I just had the same issue and removing the ".atlas" from the filename fixed it. Although that is the file type, when I open my atlas file's properties in Windows explorer, the file type is simply listed as "file" rather than atlas.
One of my plugins uses the files created by the gradle build of another project.
However gradle evaluates the plugin task before building. Is there a way to make the plugin tasks be created after the build is completed?
EDIT:
Here is the build.gradle
apply plugin: 'confluence-export'
sourceSets {
tools
}
compileToolsJava {
source += sourceSets.main.java
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
dependencies {
toolsCompile files("${System.getProperty('java.home')}/../lib/tools.jar")
}
task generateTagDoc(type: JavaExec) {
classpath = sourceSets.tools.runtimeClasspath
main = 'TagsDocumentation'
outputs.upToDateWhen { false }
}
task generateTypeDoc(type: Javadoc) {
classpath = sourceSets.tools.runtimeClasspath
source = sourceSets.main.allJava
options.docletpath = compileToolsJava.outputs.files.asList()
options.doclet = "ExtractCommentsDoclet"
options.addStringOption("Tags.java")
outputs.upToDateWhen { false }
}
confluence {
spaceKey = *****
exportAll = true
title = *******
exportUser = "******
exportPassword = ******
libraryName = ******
host = ********
}
asciidoctor {
resources {
from(sourceDir) {
include 'img/**'
}
}
}
asciidoctor.dependsOn(generateTagDoc)
generateTagDoc.dependsOn(generateTypeDoc)
generateTypeDoc.dependsOn(compileToolsJava)
So here I want my build to run which will create a folder asciidoc/html5 in the build folder with all my created html pages from my asciidoc files. My plugin goes through each file and creates a task for it and then uploads it to a website. The problem is the plugin task is evaluated before the build so the folder asciidoc/html5 hasn't been created yet. If i add a check to see if the folder has been created it will indeed remove my error however the task will still be empty. This is why i would like to know if there is a way for the plugin tasks to be created after the build is done so that the folder is created.
EDIT 2:
Here is the plugin creation:
class ConfluenceExportPlugin extends ConfluencePluginBase implements Plugin<Project> {
#Override
void apply(Project project) {
def confluenceExtension = project.extensions.create('confluence', ConfluenceExtension)
project.afterEvaluate {
if (confluenceExtension.exportAll) {
def list = []
def dir = new File("${project.buildDir}/asciidoc/html5")
if(dir.exists() && dir.isDirectory())
dir.eachFileRecurse(FileType.FILES) {
if(it.name.endsWith('.html')) {
list.add(it)
}
}
Task mainTask = project.task('confluenceExport')
mainTask.dependsOn("build")
list.each { file ->
def curName = file.name.take(file.name.lastIndexOf('.'))
ConfluenceExportTask subTask = createTask(project, "confluenceExport${curName}", confluenceExtension, file.path, curName)
mainTask.dependsOn(subTask)
}
}
else {
ConfluenceExportTask task = createTask(project, "confluenceExport", confluenceExtension, getManFile(confluenceExtension, project), confluenceExtension.title)
}
}
}
ConfluenceExportTask createTask(Project project, String taskName, def confluenceExtension, manFile, String pageTitle ){
ConfluenceExportTask task = project.task(type: ConfluenceExportTask, taskName)
task.conventionMapping.map "user", { confluenceExtension.user }
task.conventionMapping.map "password", { confluenceExtension.password }
task.conventionMapping.map "spaceKey", { confluenceExtension.spaceKey }
task.conventionMapping.map "manFile", { manFile }
task.conventionMapping.map "pageTitle", { pageTitle }
task
}
String getManFile(ConfluenceExtension configuration, Project project) {
configuration.exportSourceFilePath ?: "${project.buildDir}/asciidoc/html5/${configuration.libraryName}.html"
}
}
I have build.gradle file:
...
jar {
baseName 'dev-filename'
manifest {
attributes (
'Class-Path': configurations.runtime.collect {it.getName() }.join(' ')
'Main-Class': 'package.of.main.class'
)
}
}
...
And properties file src/main/resources/application.properties:
...
database.username=dev_user
database.password=dev_password
...
How to create tasks (dev and prod) to build jar file and update values in the property file?
UPD1:
I've tried next, but it doesn't work:
...
jar {
baseName 'dev-filename'
manifest {
attributes (
'Class-Path': configurations.runtime.collect {it.getName() }.join(' ')
'Main-Class': 'package.of.main.class'
)
}
ant.propertyfile(file: 'application.properties') {
entry(key: 'database.username', value: 'new_username')
entry(key: 'database.password', value: 'new_password')
}
}
...
Try to process your resources before packing it.
processResources {
filesMatching('*.properties') {
filter( ReplaceTokens, tokens:['foo' : 'bar'])
}
}
I solved my problem like:
task jarProd(type: Jar) {
doFirst {
ant.property(file: "build/resource/main/config/application.properties") {
entry(key: "database.username", value: "prod_user",
entry(key: "database.password", value: "prod_password"
}
}
baseName 'myapp'
manifest {
attributes (
'Class-Path': configurations.runtime.collect {it.getName() }.join(' ')
'Main-Class': 'package.of.main.class'
)
}
with jar
}
task zipProd(dependsOn: jarProd, type: Zip) {
baseName 'prod/myapp'
from ('build/libs') {
include("myapp.jar")
}
from configurations.runtime
}
task jarTest(type: Jar) {
doFirst {
ant.property(file: "build/resource/main/config/application.properties") {
entry(key: "database.username", value: "test_user",
entry(key: "database.password", value: "test_password"
}
}
baseName 'myapp'
manifest {
attributes (
'Class-Path': configurations.runtime.collect {it.getName() }.join(' ')
'Main-Class': 'package.of.main.class'
)
}
with jar
}
task zipTest(dependsOn: jarTest, type: Zip) {
baseName 'test/myapp'
from ('build/libs') {
include("myapp.jar")
}
from configurations.runtime
}
task zip {
dependsOn zipTest
dependsOn zipProd
}
As a result, I have 2 same zip files with different credentials for DB
In Gradle i can get project info (dependencies, artifact, and group id's) on Groovy like this:
class TestPlugin implements Plugin<Project> {
#Override
void apply(Project project) {
def example = project.tasks.create("example") << {
def dep = project.configurations.runtime.allDependencies
def info = project.configurations.runtime.getName()
def g = project.configurations.runtime.getAllArtifacts()
}
How can i get this on Java ?
You can add a task that will write whatever values you like out to a java Properties file, like so:
apply plugin: 'java'
apply plugin: 'application'
def generatedResourcesDir = new File(project.buildDir, 'generated-resources')
tasks.withType(Jar).all { Jar jar ->
jar.doFirst {
def props = new Properties()
props.foobar = 'baz'
generatedResourcesDir.mkdirs()
def writer = new FileWriter(new File(generatedResourcesDir, 'build.properties'))
try {
props.store(writer, 'build properties')
writer.flush()
} finally {
writer.close()
}
}
}
sourceSets {
main {
resources {
srcDir generatedResourcesDir
}
}
}
mainClassName = 'BuildProps'
Notice that a directory is created in the build output directory of the root project (called generated-resources, though you can call it whatever you want, within reason). The properties file is then written to this directory as a result of running the custom task before any jar task. Finally, the generated-resources directory is added to the resources source set. This means it will become a resource within the generated jar file and as such can be accessed like any other resource; for example:
import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;
class BuildProps {
public static void main(String[] args) {
try (InputStream inputStream =
BuildProps.class.getClassLoader().getResourceAsStream("build.properties")) {
Properties props = new Properties();
props.load(inputStream);
System.out.println("Build properties:");
System.out.println("foobar=" + props.getProperty("foobar", ""));
} catch (IOException e) {
e.printStackTrace();
}
}
}
which will print:
Build properties:
foobar=baz
As for your specific desired properties, you could set them like this: replace the line props.foobar = 'baz' with the following
def dependenciesProp = ''
for (def dependency : project.configurations.runtime.allDependencies) {
dependenciesProp += dependency.toString() + ','
}
props.dependencies = dependenciesProp
props.runtimename = project.configurations.runtime.name
def artifactsProp = ''
for (def artifact : project.configurations.runtime.allArtifacts) {
artifactsProp += artifact.toString() + ','
}
props.artifacts = artifactsProp