Run gradle commands from other directory - java

I have folder "A" and folder "B"
Folder "B" is having gradle code
I want to run gradle clean and gradle build command from folder "A" of folder "B"
How do I do this?

You should use the "start directory" parameter (-p, --project-dir : see Environment options)
I think the other available parameter -b --build-file could work as well, but its main usage is when your build script filename differs from default build.gradle.

Use the -b parameter(i.e. --build-file)
cd A
gradle -b ../B/build.gradle

for me this works
gradle clean build --console=plain -p ${projectPath}
Where:
clean: Is to "clean" build/libs folder.
Console plain: To show only build results in console
// Compilando
sh(label: 'Compilando', script: "gradle clean build --console=plain -p ${rutaTemp}")
sh(label: 'Listando archivos', script: "ls -lart ${rutaTemp}")

This is harder than it looks.
Dispite setting different values for --project-dir , --gradle-user-home , --build-file
No matter what you do, when you "println project.projectDir" from your build.gradle
script it will ALWAYS report back the directory in which "build.gradle" lives.
I wanted to re-arrange things in gradle because gradle pollutes your root directory
with a lot of junk! Uncle Bob of "Clean Code" (Robert C. Martin) would probably refer to this behavior as "rude code".
I finally figured it out after searching around all day.
Here is my project structure:
<root_folder>
|
+--[ .git ]
+--[ .gitignore ]
|
+--[-]src/main/java
| |
| +--Main.java
|
+--[-]RUN_ON_CMD
|
+--[-]Gradle
+--[ build.gradle ]
+--[ RUN.sh ]
|
+--[-]GENERATED
.gitignore :
GENERATED/
build.gradle :
apply plugin: 'java'
apply plugin: 'application'
println "[project.projectDir]:"
println project.projectDir
mainClassName = 'Main'
sourceSets {
main {
java {
//:Because "build.gradle" lives in:
//:<root>\RUN_ON_CMD\Gradle\GENERATED\
srcDir '../../../src/main/java'
}
}
}
RUN.sh
build_gradle=$( realpath build.gradle )
echo $build_gradle
current_directory=$( realpath "." )
echo $current_directory
generated=${current_directory}/"GENERATED"
echo $generated
cp $build_gradle $generated/"build.gradle"
gradle run -b $generated/"build.gradle" -g $generated --no-daemon
main.java
public class
Main{
public static void
main(
String[] args
){
System.out.println("[MAIN]");
}
}
To Run:
Do a "git bash here" inside the "Gradle" folder.
Then type:
./RUN.sh
And hit ENTER
My output: (TDD_JAVA == root_folder )
JMIM#DESKTOP-JUDCNDL MINGW64 /c/DEV/REPO/GIT/TDD_JAVA/RUN_ON_CMD/Gradle (master)
$ ./RUN.sh
/c/DEV/REPO/GIT/TDD_JAVA/RUN_ON_CMD/Gradle/build.gradle
/c/DEV/REPO/GIT/TDD_JAVA
/c/DEV/REPO/GIT/TDD_JAVA/RUN_ON_CMD/Gradle
/c/DEV/REPO/GIT/TDD_JAVA/RUN_ON_CMD/Gradle/GENERATED
To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: https://docs.gradle.org/5.4.1/userguide/gradle_daemon.html.
Daemon will be stopped at the end of the build stopping after processing
> Configure project :
[project.projectDir]:
C:\DEV\REPO\GIT\TDD_JAVA\RUN_ON_CMD\Gradle\GENERATED
> Task :run
[MAIN]
BUILD SUCCESSFUL in 8s
2 actionable tasks: 1 executed, 1 up-to-date
All the junk generated by gradle is put into
the "GENERATED" folder. Then my .gitignore makes sure not to commit any of that junk.

Related

snapscraft snap.yaml for java apps using swing ui on ubuntu

I have a basic java app using java.awt Swing UI and I want to create a snap for it so when it's installed there is a launcher available and my UI Launches. I use gradle and the jar task to create a jar which works fine.
Naturally I have in a class that when called loads my app just fine:
package com.foo
class Bar() {
static void main(String... args) { launchUI() }
}
I created a snap folder at the root of the project and a snap.yaml where i followed the instructions on https://snapcraft.io/docs/java-applications so i have a snap yaml that produces a snap file which also installs fine:
name: deepthought
base: core18
version: '0.0.7'
summary: ""
icon: gui/foo.png
description: |
Computes the ultimate answer for life the universe and everything
grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots
# This doesn't work
#apps:
# htmldoc:
# command: desktop-launch $SNAP/bin/foo.sh
## desktop: share/applications/htmldoc.desktop
# plugs: [home, network, x11]
parts:
foopart:
plugin: gradle
source: https://github.com/bsautner/foo.git
source-type: git
gradle-options: [] # suppress running of tests and run the war task
gradle-output-dir: build/libs
I've spent quite some time trying to figure out:
If i create a shell script to run a java -jar foo.jar command it ends up in the /snap directory but it's not on the users path so they can't get to it
I've tried creating launchers but always get an error that my launcher can't be found, if i put it in my root folder as /bin/launch.sh snap can't find it and if i put it in the snap/bin/ folder i also get errors to not put things in the snap folder
When I do install my snap, i don't see where it puts my jar i want to execute, so i can't write a script that does that
I'd really appreciate if anyone can share a working snap.yaml for a java program with a launcher and if there is any mention of a path to something that you note where those files are in relation to the path of the /snap/snap.yaml file
ok figured it out - the docs don't say this but the files are relative to the root of the project so even though the yaml says this is the launch
apps:
cmd3:
command: usr/bin/foo.sh
foo.sh should be in the root of the project and the orginize section here moves it to the bin dir
foo:
plugin: gradle
source-type: local
source: .
build-packages:
- openjdk-11-jdk
stage-packages:
- openjdk-11-jdk
- x11-utils
organize:
${SNAPCRAFT_PART_BUILD}/jg-snap: usr/bin/foo.sh
The jar is in the /snap/foo/current/usr/jar directory

Jenkins - mvn not found

Hello I'm new to jenkins and getting this issue. I'm using jenkins in windows azure
mvn clean package /var/lib/jenkins/workspace/vcc#tmp/durable-b5407f14/script.sh: 2:
/var/lib/jenkins/workspace/vcc#tmp/durable-b5407f14/script.sh: mvn:
not found.
Jenkinsfiles:
node {
stage('init') {
checkout scm
}
stage('build') {
sh '''
mvn clean package
cd target
cp ../src/main/resources/web.config web.config
cp todo-app-java-on-azure-1.0-SNAPSHOT.jar app.jar
zip todo.zip app.jar web.config
'''
}
stage('deploy') {
azureWebAppPublish azureCredentialsId: env.AZURE_CRED_ID,
resourceGroup: env.RES_GROUP, appName: env.WEB_APP, filePath: "**/todo.zip"
}
}
can any body help me how can I resolve this mvn issue.
P.S I'm following this tutorial
https://learn.microsoft.com/en-us/azure/jenkins/tutorial-jenkins-deploy-web-app-azure-app-service
You may try to add maven tool to your pipeline:
tools {
maven 'M3'
}
stages {
stage('init') {
checkout scm
}
stage('build') {
sh '''
mvn clean package
cd target
cp ../src/main/resources/web.config web.config
cp todo-app-java-on-azure-1.0-SNAPSHOT.jar app.jar
zip todo.zip app.jar web.config
'''
}
stage('deploy') {
azureWebAppPublish azureCredentialsId: env.AZURE_CRED_ID,
resourceGroup: env.RES_GROUP, appName: env.WEB_APP, filePath: "**/todo.zip"
}
}
I add this line right before sh command in the build stage : def mvnHome = tool name: 'Apache Maven 3.6.0', type: 'maven'
and instead of mvn you should use ${mvnHome}/bin/mvn
thank this youtube film to help me.
pipeline{
stage('com'){
def mvnHome = tool name: 'Apache Maven 3.6.0', type: 'maven'
sh "${mvnHome}/bin/mvn -B -DskipTests clean package"
}
}
You may wanna check if Jenkins has the pipeline-maven plugin installed.
If you don't have it, search and install the pipeline-maven plugin.
Once the plugin is installed, you can use maven as follows
node{
stage('init'){
//init sample
}
stage('build'){
withMaven(maven: 'mvn') {
sh "mvn clean package"
}
}
}

CMake: Generate a jar file using multiple jar dependencies (out of source build)

I have spent a few days now to do the following with CMake (3.5.2). Using an out of source build, create a jar file relying on multiple external jar dependencies. In my manifest, I want only to specify Main-Class, nothing else.
This is what I have in my croco/owl2gdb/CMakeLists.txt directory:
cmake_minimum_required (VERSION 3.5)
find_package(Java REQUIRED)
include(UseJava)
project (owl2gdb)
SET(CLASSPATH ${CMAKE_CURRENT_SOURCE_DIR}/lib/commons-lang3-3.3.2.jar:${CMAKE_CURRENT_SOURCE_DIR}/lib/concurrentlinkedhashmap-lru-1.4.2.jar:${CMAKE_CURRENT_SOURCE_DIR}/lib/HermiT.jar)
list(APPEND CMAKE_JAVA_INCLUDE_PATH ${CLASSPATH})
SET(SOURCE
${CMAKE_CURRENT_SOURCE_DIR}/src/owl2gdb/OWL2GDB.java
${CMAKE_CURRENT_SOURCE_DIR}/src/graphdatabase/GraphDatabase.java
${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/CPPFileWriter.java
${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/CppHeaderFileWriter.java
${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/CppSourceFileWriter.java
${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/CppTextTemplate.java
${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/GraphDatabaseHeaderWriter.java
${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/GraphDatabaseSourceWriter.java
${CMAKE_CURRENT_SOURCE_DIR}/src/knowledge/MyOntology.java
${CMAKE_CURRENT_SOURCE_DIR}/src/knowledge/MyOWLClass.java
${CMAKE_CURRENT_SOURCE_DIR}/src/knowledge/MyOWLDataProperty.java
${CMAKE_CURRENT_SOURCE_DIR}/src/knowledge/MyOWLObjectProperty.java
${CMAKE_CURRENT_SOURCE_DIR}/src/knowledge/MyOWLOntology.java
${CMAKE_CURRENT_SOURCE_DIR}/src/tool/Util.java)
set(JAVA_JAR_ENTRY_POINT owl2gdb/OWL2GDB)
add_jar(owl2gdb ${SOURCE})
add_custom_command(TARGET owl2gdb
POST_BUILD
COMMAND jar u0fm owl2gdb.jar ${CMAKE_CURRENT_SOURCE_DIR}/META-INF/MANIFEST.MF ${SOURCE}
)
In my root directory, I do:
mkdir _build
cd _build
cmake ..
make
owl2gdb.jar is indeed generated in _build/owl2gdb/owl2gdb.jar. Then
cd owl2gdb
java -jar owl2gdb.jar
gives me
Exception in thread "main" java.lang.NoClassDefFoundError: com/martiansoftware/jsap/StringParser
Basically, the external jars are not included in my owl2gdb.jar. I don't really know where to go from here. Is there a problem with cmake not properly using CMAKE_JAVA_INCLUDE_PATH? What is missing in my CMakeLists.txt?
a jar tf owl2gdb.jar returns:
META-INF/
META-INF/MANIFEST.MF
tool/Util.class
graphdatabase/GraphDatabase$1.class
graphdatabase/GraphDatabase.class
cpp/CPPFileWriter.class
cpp/GraphDatabaseHeaderWriter.class
cpp/CppHeaderFileWriter.class
cpp/CppTextTemplate.class
cpp/CppSourceFileWriter.class
cpp/GraphDatabaseSourceWriter.class
owl2gdb/OWL2GDB.class
knowledge/MyOWLClass.class
knowledge/MyOWLDataProperty.class
knowledge/MyOntology.class
knowledge/MyOWLObjectProperty.class
knowledge/MyOWLOntology.class
usr/local/me/github/croco/owl2gdb/src/owl2gdb/OWL2GDB.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/graphdatabase/GraphDatabase.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/cpp/CPPFileWriter.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/cpp/CppHeaderFileWriter.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/cpp/CppSourceFileWriter.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/cpp/CppTextTemplate.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/cpp/GraphDatabaseHeaderWriter.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/cpp/GraphDatabaseSourceWriter.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/knowledge/MyOntology.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/knowledge/MyOWLClass.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/knowledge/MyOWLDataProperty.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/knowledge/MyOWLObjectProperty.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/knowledge/MyOWLOntology.java
usr/local/me/github/croco/owl2gdb/src/owl2gdb/src/tool/Util.java
This is the only thing I have in my manifest:
Main-Class: owl2gdb.OWL2GDB

run.sh - package X does not exist

I'm running ./run.sh from Alfresco All-in-One extension but I got the error:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:3.2:compile
(default-compile) on project repo-amp: Compilation failure:
Compilation failure:
[ERROR]
/home/user/signextension/sign/repo-amp/src/main/java/pt/empt/sign/fields/CreateFields.java:[3,25]
package com.itextpdf.text does not exist
[ERROR]
/home/user/signextension/sign/repo-amp/src/main/java/pt/empt/sign/fields/CreateFields.java:[4,25]
package com.itextpdf.text does not exist
[ERROR]
/home/user/signextension/sign/repo-amp/src/main/java/pt/empt/sign/fields/CreateFields.java:[5,1]
package com.itextpdf.text.pdf does not exist
My run.sh:
#!/bin/bash
# Downloads the spring-loaded lib if not existing and runs the full all-in-one
# (Alfresco + Share + Solr) using the runner project
springloadedfile=~/.m2/repository/org/springframework/springloaded/1.2.3.RELEASE/springloaded-1.2.3.RELEASE.jar
if [ ! -f $springloadedfile ]; then
mvn validate -Psetup
fi
MAVEN_OPTS="-javaagent:$springloadedfile -noverify -Xms256m -Xmx2G" mvn clean install -Prun
I try this:
#!/bin/bash
# Downloads the spring-loaded lib if not existing and runs the full all-in-one
# (Alfresco + Share + Solr) using the runner project
springloadedfile=~/.m2/repository/org/springframework/springloaded/1.2.3.RELEASE/springloaded-1.2.3.RELEASE.jar
itextpdffile=~/.m2/repository/com/itextpdf/itextpdf/5.5.7/itextpdf-5.5.7.jar
if [ ! -f $springloadedfile ] && [ ! -f $itextpdffile ]; then
mvn validate -Psetup
fi
MAVEN_OPTS="-javaagent:$springloadedfile -javaagent:$itextpdffile -noverify -Xms256m -Xmx2G" mvn clean install -Prun
But I got the error:
Failed to find Premain-Class manifest attribute in
/home/user/.m2/repository/com/itextpdf/itextpdf/5.5.7/itextpdf-5.5.7.jar
Error occurred during initialization of VM agent library failed to
init: instrument
Any help for solve this?
The itext library is not a javaagent library, check this link to learn more about java agents and what are they meant for !
The right way to add itext dependency to your project is by adding this snippet:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.7</version>
</dependency>
To your main pom.xml file (or the repo-amp pom.xml alternatively) (in the dependencies section). And of course roll back any changes you made to the run.sh file !

How do I add default JVM arguments with Gradle

I need to add default JVM options to my jar, when build with Gradle.
From the documentation I got that I have to set:
applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"]
I have not much experience with Gradle and the developer that wrote the build.gradle file wrote it different from what most websites give as examples.
Here is the build.gradle:
apply plugin: 'java'
apply plugin: 'eclipse'
version = '0.1'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.+'
compile 'placeholder'
}
task release(type: Jar) {
manifest {
attributes("Implementation-Title": "placeholder",
"Implementation-Version": version,
'Main-Class': 'placeholder.Application')
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
}
I don't know where to put the arguments. I tried putting them in different locations, but I always get:
A problem occurred evaluating root project 'placeholder'.
> No such property: applicationDefaultJvmArgs for class: org.gradle.api.tasks.bundling.Jar_Decorated
Much Thanks,
Jhonny
From the top of my head I can think of 2 options:
Option1: Do what #Ethan said, it'll likely work:
package placeholder;
//your imports
public class Application{
static {
System.getProperties().set("javafx.embed.singleThread", "true");
}
// your code
public static void main(String... args){
//your code
}
}
Option 2: Use the application plugin + default jvm values
build.gradle:
apply plugin: 'application'
//your code
applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"]
Now you can run your code 2 ways:
From gradle
$gradle run
From distribution(script). from the generated script that the application plugin will provide:
$gradle clean build distZip
Then gradle will generate a zip file somewhere under ${your.projectdir}/build. Find the zip then unzip it and under /bin you'll find a ${yourproject}.bat and ${yourproject} executables. One is for Linux/mac/unix (${yourproject}) the other one is for windows (${yourproject.bat})
Option 3 (Android Developer): Use gradle.properties to set jvm argument
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx1024m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# You can setup or customize it according to your needs and combined with the above default value.
org.gradle.jvmargs=-Djavafx.embed.singleThread=true
For more info on how to use gradle build environment on docs.gradle.org
applicationDefaultJvmArgs is provided by the Application plugin. So if you apply that plugin, the error would probably go away, and you should be able to execute the program by issuing gradle run once you set the mainClassName property to the fully qualified class name, the main method of which you want to invoke.
Using a local gradlew is the easiest.
Just append to DEFAULT_JVM_OPTS.
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m" "-XX:+AllowRedefinitionToAddDeleteMethods"'
you can use command-line with gradle taskļ¼š
class AppRun extends JavaExec {
private boolean withDebug
#Option(option = "with-debug", description = "enable debug for the process. ")
public void setDebugMode(boolean debug) {
this.withDebug = debug
}
public boolean getDebugMode() {
return this.withDebug
}
}
task run(type: AppRun) {
}
then run task with options
gradle run --with-debug
set this to Your java main Class.
static {
System.setProperty("nashorn.args", "--no-deprecation-warning");
}

Categories