no main manifest attribute error in multimodule app with gradle - java

I have multimodule java/kotlin app with Gradle.
I wanna make .jar to launch my app in a terminal: like java -jar mayApp.jar
How correct build .jar in multimodule app?
My .jar generated by IDEA is not runs when I trying in terminal due to error:
no main manifest attribute, in /Users/me/IdeaProjects/MyProject/out/artifacts/MyProject_jar/MyProject.jar
project structure:
- :ApplicationName
- :bot-app
- src/main/java/main
Main.java // psvm
- src/main/resources
- META-INF
MANIFEST.MF
build.gradle // module's build
- :data
- :utils
build.gradle // application (root) build
So, in my multimodule project the main class is located in the :bot-app module.
Each module has its own build.gradle and in the root project I have build.gradle of the app;
Module build.gradle
buildscript {
repositories {
...
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0"
}
}
apply plugin: "org.jetbrains.kotlin.jvm"
apply plugin: 'kotlin-kapt'
group 'org.my_project'
version '2.4.0'
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
}
And it is my root build.gradle
buildscript {
repositories {
...
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0"
}
}
apply plugin: 'java'
tasks.withType(Jar) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes["Main-Class"] = "main.Main"
}
}
As u see, I added
tasks.withType(Jar) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes["Main-Class"] = "main.Main"
}
}
but it is not works for me. With 1 module it worked, but after refactoring to multimodule, no.
How build .jar in multimodule app?
----
UPD:
if I delete the .gradle folder in the root project and then try to run the app via IDEA it works well. But when I build artifacts via IDEA, jar is created but not works with the error:
no main manifest attribute
And each next build in IDEA is failed with error:
`Execution failed for task ':bot-app:jar'.
Entry META-INF/bot-app.kotlin_module is a duplicate but no duplicate handling strategy has been set.`
If I delete .gradle again, build in IDEA works well.

Need to delete:
tasks.withType(Jar) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes["Main-Class"] = "main.Main"
}
}
This code is extra for me.
Then create MANIFEST.MF at the root of the project.
Note: you must create it via file->project structure->artifact->+
and in the field Directory for META-INF/MANIFEST.MF: u must set root folder location (NOT module folder, but root)
Remove .gradle, clean, and build artifacts.

Related

Tried importing a library and ended up with Plugin with id 'com.android.application' not found

I just tried importing a library into my android project and ended up with this Plugin with id 'com.android.application' not found. error. I was supposed to import "Filters.jar" file from jhlabs but accidentally imported "gradle-wrapper.jar" of another project . I am stuck here and nothing seems to work. I also accidentally deleted my project level build.gradle file, Which contained the following code :
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I was trying to import the library following this answer,
https://stackoverflow.com/a/35369267/9504498
I messed up apparently I have tried many solutions but nothing is working. Please, help me fix this error I am stuck here.
How can I get the project level build.gradle file back? and how can I fix this Plugin with id 'com.android.application' not found.?
Any help will be appreciated, Thanks.
You need to re-add your build.gradle to your project by following the structure of sample project which you can create from Android Studio.
Usually, the project structure is like the following:
app -> (your app module)
gradle
build.gradle -> (project build.gradle)
gradlew
gradlew.bat
local.properties
settings.gradle
The following error:
Plugin with id 'com.android.application' not found.
is because gradle can't found the plugin from your project build.gradle, so add the following code to your project build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
For adding a jar library, as #daniel-nugent says, try:
How to manually include external aar package using new Gradle Android Build System
Tried many options but nothing really worked and finally, created a new project with exact name as the previous one and the exact package name, then copied the "main" from the previous project to the new project and edited the app level build.gradle by copying from the previous project and things are back to normal now. Will try importing the .jar library by following #daniel-nugent method.

How to put the res files from the project to the jar in the same folder?

I have problem with gradle.
I need the res folder had exactly the same location as in the project in order that would be executable file addressed to the resource in the same way as in the "unbuild" project.
my project file tree:
Project
+--src
+--main
|--kotlin
+--res
In the project resource usage I address in the following way:
var serviceAccount = FileInputStream("src/main/res/my_resource_file.json")
And I need when assembled the project location of the resource file saved in the jar file
my build.gradle file:
group 'Bot'
version '1.0'
buildscript {
ext.kotlin_version = '1.1.2'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
ext {
jsoup = "1.10.2"
//...
firebase_admin = "5.0.0"
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
//...
compile "com.google.firebase:firebase-admin:$firebase_admin"
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin/'
test.java.srcDirs += 'src/test/kotlin/'
main {
resources {
srcDirs = ["src/main/res"]
}
}
}
jar {
manifest {
attributes 'Main-Class': 'com.my.bot.MainKt'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
I suspect that you need to configure this section jar{//...}, but i don't know what did is in gradle
Normally you would reference such as resource from: src/main/resources as the source folder which then is copied auto copied into your JAR by Gradle. You then use the Classloader to read the resources as a stream.
If I placed a file at src/main/resources/my_resource_file.json it would be loaded via:
// relative to the current class package location
val input = javaClass.getResourceAsStream("/my_resource_file.json")
// or use system classloader
val input = ClassLoader.getSystemClassLoader().getResourceAsStream("my_resource_file.json")
You are instead doing it in a non standard way by using res as the directory name instead of resources and by trying to access it as a file instead of a classpath resource. In Gradle docs (similarly for Maven) they explain the file layout: Gradle Java Project Layout

Add OkHttp to my project

I am trying to add OkHttp to my project, i download both OkHttp and Okio library and add it to the libs directory.
than i add the compile methods:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I try to Build the App and i get this error:
Error:(27, 1) A problem occurred evaluating root project 'APP'.
> Could not find method compile() for arguments [com.squareup.okhttp3:okhttp:3.4.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Any idea what can be the problem? i am using Android Studio om mac OS
Delete this line of your root gradle :
compile 'com.squareup.okhttp3:okhttp:3.4.1'
And Add this to your dependencies's app's module build.gradle
There are basically two build.gradle files in our project.
Top level gradle (can be found in the project directory)
Module level gradle (present inside the app folder of the project)
Here the problem is you are compiling the okHttp
compile 'com.squareup.okhttp3:okhttp:3.4.1'in the Top level gradle file try to place it in your Module level gradle. Also if you are importing a library using gradle you don't have to add it in the lib folder explicitly
Google gradle documentation

How do i fix my dependencies in my build.gradle file

I am new to gradle, i need to configure my build.gradle file . Am using selenium webdriver and i have list of .jar files. how do i include this jar files as dependencies in my build.gradle file?. i have this .jar in a folder called lib in my package. and i have
dependencies {
compile fileTree(dir: 'lib', includes: '*.jar')
}
but i keep having the error below:
FAILURE: Build failed with an exception.
Where:Build file '/home/ola/workspace/build.gradle' line: 20
What went wrong:
A problem occurred evaluating root project 'workspace'. Cannot cast object '*.jar' with class 'java.lang.String' to class 'java.lang.Iterable'
please can anyone point me to how to write dependencies for a webdriver project using gradle.This is the path to my lib folder: "/home/user/workspace/mainsite_automation/src/autoTest/lib/"
Thanks
You just need to specify the dependencies repository and the selenium webdriver dependencies so you will end up with a build.gradle similar to this:
apply plugin: 'java'
apply plugin: 'jetty'
repositories {
mavenCentral()
}
sourceSets {
selenium
}
dependencies {
seleniumCompile 'junit:junit:4.11'
seleniumCompile 'org.seleniumhq.selenium:selenium-java:2.30.0'
}
task jettyDaemon(type: org.gradle.api.plugins.jetty.JettyRun) {
daemon = true
}
task selenium(type: Test, dependsOn: jettyDaemon) {
testClassesDir = sourceSets.selenium.output.classesDir
classpath = sourceSets.selenium.runtimeClasspath
}
for Eclipse, you can add selenium dependencies to the classpath adding this to your build.gradle:
eclipse {
classpath {
plusConfigurations += configurations.seleniumCompile
}
}
then you can use grade clean selenium to rebuild your project.
Sources:
https://docs.gradle.org/current/userguide/artifact_dependencies_tutorial.html
http://www.dreamchain.com/gradle-selenium-webdriver-task/

Gradle generated Jar does not include gwt.xml file?

I created a small library project with the following build.gradle:
apply plugin: 'java'
apply plugin: 'gwt-base'
apply plugin: 'eclipse'
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url new File(rootProject.projectDir.parentFile, 'repo').toURI()
// url "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
dependencies {
classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6'
}
}
gwt {
gwtVersion='2.7.0'
}
The folder structure looks like this:
/library
/library/Library.gwt.xml
/library/client/HelloWorldWidget.java
The sources are taken from here.
When I perform a gradle buildgradle generates a jar file which does not contain the sources and also does not contain the gwt.xml module file.
How can I force gradle to include the sources and the gwt.xml file in the generated jar?
Here is the solution to include the *.java files use:
jar {
from('src/main/java') {
include '**/*.java'
}
}
The include any other resources like gwt.xml files put them into:
/src/main/resources
Alternatively you can use:
jar {
from project.sourceSets.main.allSource
from project.sourceSets.main.output
}
When using the Java plugin, Gradle assumes that the project has the standard structure, i.e. 'src\main\java' & 'src\test\java'. Therefore when executing the build tasks it simply doesn't find any of those directories.
The best way to fix this will be to define your project structure by modifying the existing source sets, which define where your sources are:
sourceSets {
main {
java {
include '/library/**'
}
}
test {
java {
include '/test/sources/directory/**'
}
}
}

Categories