No Main Maifest attribute Gradle - java

I have read all the other threads about this problem and applied all the solutions I could find. Nothing helped. When I run the gradle.build task I get a .jar file. But when running the file I get no main manifest attribute, in DiscordBotJDA-1.0.jar
Can annyone provide help?
Thanks a lot!
Here is my gradle.buidl file:
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'Timbo' at '7/13/16 2:08 PM' with Gradle 2.9
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/2.9/userguide/tutorial_java_projects.html
*/
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'start.StartUp'
version = '1.0'
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'MCI_Bot',
'Implementation-Version': version,
'Main-Class': 'start.StartUp'
manifest.attributes("Main-Class": 'start.startUp')
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.13'
compile 'net.dv8tion:JDA:2.1.3_327'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
I am using eclipse in case it matters.

Were you expecting that your "fatJar" task would be doing that? Just defining the task doesn't put it into the task execution tree. You have to either run the task directly, or specify a task dependency relationship including your task. For instance, saying that another task depends on it.
You might be better off just specifying a "jar" configuration block, which will configure the already existing "jar" task, which already has proper dependency relationships defined.
Read the User Guide for examples of configuring the "jar" task.
It might be more convenient to download the Gradle distribution, which provides the PDF of the User Guide, which might be easier to search.

Related

JavaFX FatJar using Gradle and IntelliJ

I've tried everything by now, so I hope someone in here can tell me more...
Im trying to produce an executable .jar from a IntelliJ Gradle JavaFX project. I used the standard setup that IntelliJ provided, I changed the Gradle.build file however.
The new file I got from here: Non-Modular Gradle (openjfx.io)
I have a main class that has some basic code in it and a launcher class that does not extend Application and is specified as the Main class in the jar manifest.
For now I only use javafx.controls and basically everything is as the example they provided here.
When doing the ./gradlew jar command I get the error:
no module-info.java found
Which - as I understand - is not required if I use the Non-Modular approach?
However if I add it I get the error:
Entry module-info.class is a duplicate but no duplicate handling strategy has been set.
I tried every other option out there, all of them lead to either the 2. error or the jar was produced but not executable due to the fact that it can't find the Application class...
Any help is greatly appreciated.
I just want to point out that I've never really used Gradle before and have never formally learned any coding, but can fiddle my way around usually.
For the sake if it my build file:
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.9' // this is old right?
}
repositories {
mavenCentral()
}
dependencies {
/* uncomment for cross-platform jar: */
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:win"
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:linux"
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:mac"
}
javafx {
version = "16"
modules = [ 'javafx.controls' ]
}
mainClassName = 'main.class.with.Code'
jar {
manifest {
attributes 'Main-Class': 'main.class.with.Launcher'
}
from {
// this is what causes the module duplicate error I think (at least it did in my other tries)
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}

Gradle - cannot load a Java Class even when packed within the jar

My gradle build file is
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building a CLI application
id 'application'
}
apply plugin: 'java'
jar {
from configurations.runtime
manifest {
attributes(
'Created-By':'Gmack',
'Main-Class':'myapprunner.App',
'Class-Path':'mydaos-1.0.jar'
)
}
}
allprojects{
repositories {
jcenter()
}
}
subprojects {
version = '1.0'
apply plugin: 'java'
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:27.1-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
// Compile Project for dependency
compile project(':mydaos')
}
application {
// Define the main class for the application
mainClassName = 'myapprunner.App'
}
When I run the app using java -jar myapprunner.jar
I get a ClassNotFoundException
Caused by: java.lang.ClassNotFoundException: com.mydaos.Library
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
I can confirm that the jar has been packed. Not sure why this is not picking things up.
Any help would be appreciated.
Thanks,
Plugin java is being applied twice and com.mydaos.Library is likely being pulled in from compile project(':mydaos') (or 'Class-Path':'mydaos-1.0.jar'). Would assume the project does not build or the class path is wrong.
Dependency classes (projects/external jars) aren't packed inside your jar by default.
You are using the application plugin which bundles your classes, your dependencies and an execution script in a zip so you should use that. The plugin also adds a "run" task to your project to run your main class via gradle for development purposes. See the application plugin docs for more info
If you want to pack your dependencies inside your jar (known as an uber jar) see here. I suggest you stop using the application plugin if you do this
'Class-Path':'mydaos-1.0.jar'
This assumes that mydaos-1.0.jar is in the same folder you are running java -jar ... from which is likely not the case

Unable to build gradle project in eclipse?

After cleaning when i try to build gradle i get an error in the console saying:
package org.json does not exist import org.json.JSONObject;
cannot find symbol
symbol : class JSONObject
there are red marks in the java file at all places where jsonobject and json array exists.
I have put the folder web inf/lib that contains all the jar files inside the src/main/webapp directory that i have created.
currently the contents of my build.gradle file are:
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'i2cdev001' at '14/11/18 3:11 PM' with Gradle 2.14.1
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/2.14.1/userguide/tutorial_java_projects.html
*/
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'war'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.21'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
Note: Also in the project properties i am unable to see any jar files under the Web App Libraries in the java build path tab. I can see only access rules:no rules defined and native library locations:(none)
As your project is gradle project, Adding jar manually wont work.. You have to mention path where you have kept all your jar files in your build.gradle file.
Mention your jar file path in repository under flatDir {}:
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
Then you have to add which jar from that folder you have mentioned above (ie libs)
dependencies {
compile 'gson-0.1.0'
}

Gradle's Maven Publish plugin not publishing POM or proper version to Maven Local

I am trying to get the Gradle Maven Publish Plugin to publish a snapshot version of my Java library to my local Maven repo such that:
The version of the jar is 1.0.0.SNAPSHOT-<timestamp>, where <timestamp> is the current system time in millis (similar to something like System.currentTimeInMillis()); and
I log to STDOUT/console the full name of the jar being published, including the version above; and
A properly-formatted pom.xml is published to Maven local alongside the jar, so that any other Gradle/Maven projects can "pull it down" locally and fetch its transitive dependencies properly
My best attempt so far:
plugins {
id 'java-library'
id 'maven-publish'
}
dependencies {
compile(
'org.hibernate:hibernate-core:5.0.12.Final'
,'com.fasterxml.jackson.core:jackson-core:2.8.10'
,'com.fasterxml.jackson.core:jackson-databind:2.8.10'
,'com.fasterxml.jackson.core:jackson-annotations:2.8.0'
)
testCompile(
'junit:junit:4.12'
)
}
repositories {
jcenter()
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
group 'com.me'
jar {
baseName = 'my-lib'
version = '1.0.0-SNAPSHOT'
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
However, with this setup, when I run ./gradlew publishToMavenLocal:
I do see the jar being deployed to ~/.m2/repository/com/me/my-lib/ but without a pom.xml and no 1.0.0.SNAPSHOT version appended to it
I don't even know how/where I would append the timestamp onto the version
I don't even know how/where I would do a println(...) to report the full name of the jar being published
Any ideas?
Regarding #3, To install your artifact to a local repository you do not need the maven-publish plugin, rather the maven plugin
See The Maven plugin documentation, specifically the Tasks section and the Installing to the local repository section with it, you can run gradle clean build install
It works for me with a build.gradle file as simple as this
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'maven'
Note, if you need to publish something other then the default generated jar then you need to change the archives configuration
Regarding #1 appending the timestamp, move the version line outside the jar clause and change it from
version = '1.0.0-SNAPSHOT'
to
version = "1.0-SNAPSHOT-${System.currentTimeMillis()}"
This is using Groovy GString (AKA string interpolation - note the change from single quotes to double quotes) to append the current time in millis to the version
Last but not least, regarding #2 printing the jar full name append the following to the build.gradle file
install.doLast {
println jar.archiveName
}
Essentially we're appending to the install task (the one executed in the top of my answer) a println of the jar configuration's archiveName (see here if you want something else)
So all in all my build.gradle file looks like this:
group 'com.boazj'
version "1.0-SNAPSHOT-${System.currentTimeMillis()}"
apply plugin: 'java'
apply plugin: 'maven'
install.doLast {
println jar.archiveName
}

Add parameter to a Gradle plugin

I'm new to Gradle and I'm working on a Liferay project. I'm trying to use the Liferay javadoc gradle plugin without success. I'm working on the Liferay IDE (Eclipse) and I already have the javadoc task available to execute. The problem is, after the excution (which completes successfully) I can't find the created docs.
I read the documentation which says there is destinationDir property but I'm unable to set it using Gradle.
I tried following this SO question in order to create a custom Gradle task but without success.
How can I set the destinationDir in order to get the generated docs?
Edit:
The (automatic generated) settings.gradle is:
buildscript {
dependencies {
classpath group: "com.liferay", name: "com.liferay.gradle.plugins.workspace", version: "1.5.0"
classpath group: "net.saliman", name: "gradle-properties-plugin", version: "1.4.6"
}
repositories {
maven {
url "https://cdn.lfrs.sl/repository.liferay.com/nexus/content/groups/public"
}
}
}
apply plugin: "net.saliman.properties"
apply plugin: "com.liferay.workspace"
I'm pretty sure that com.liferay.gradle.plugins.workspace includes the javadoc plugin. Furthermore, Liferay also automatically creates an empty build.gradle where I put:
apply plugin: 'java'
task api(type: Javadoc) {
source = sourceSets.main.allJava
destinationDir = new File(buildDir, "/api")
}
Launching the api Gradle task the javadoc plugin is not executed

Categories

Resources