How to add command line properties with gradle bootRun? - java

24.3 Application Property Files uses the following command to add an application property spring.config.name:
$ java -jar myproject.jar --spring.config.name=myproject
How can I do this with gradle bootRun?

BootRun task extends JavaExec task: https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/gradle-plugin/api/org/springframework/boot/gradle/tasks/run/BootRun.html
Since Gradle 4.9, the command line arguments can be passed with --args. For example, if you want to launch the application with command line arguments foo --bar, you can use:
gradle bootRun --args='--spring.config.name=myproject'

Related

Gradle bootRun not working with SPRING_PROFILES_ACTIVE but works with spring.profiles.active

MacOS Monterey.
Spring Boot 2.4.5.
Gradle 7.4.2
The question is pretty simple.
This one doesn't work:
SPRING_PROFILES_ACTIVE=local ./gradlew bootRun
This one works:
./gradlew bootRun --args='--spring.profiles.active=local'
Under "works", I understand that the profile is being successfully passed to Gradle task.
What I have read is that SPRING_PROFILES_ACTIVE - is OS environment variable, while spring.profiles.active is System property.
Still not clear why the option SPRING_PROFILES_ACTIVE=local ./gradlew bootRun doesn't work.
Thank you.
Break this into multiple lines
$ export SPRING_PROFILES_ACTIVE=local
$ ./gradlew bootRun

Gradle commandline pass org.gradle.jvmargs

How can I pass this params to gradle command
e.g.
./gradlew clean build org.gradle.jvmargs=-Xmx2g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
gives error as
* What went wrong:
Task 'org.gradle.jvmargs=-Xmx2048m' not found in root project
What you have written works if you put it in a gradle.properties file. If you want to use it on the command line, you first have to add a -D (see command line interface in the user guide) but also quote the argument as it contains spaces. E.g.
./gradlew clean build "-Dorg.gradle.jvmargs=-Xmx2g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError" -Dfile.encoding=UTF-8
-Dorg.gradle.jvmargs="-Xmx8192M -Dkotlin.daemon.jvm.options\\=\"-Xmx8192M\""
works for me

UnsupportedOperationException: No format processor for org.jboss...MavenResolvedArtifact was found

I'm creating an application to automatically generate resources for a launcher, this requires automatically resolving maven dependencies, but I'm getting an UnsupportedOperationException while running JBoss Shrinkwrap Resolver
I'm running this inside a docker container, to avoid the local repository caching, it works outside of the container on the host, but I'm unsure what is missing inside the container.
My resolver config is a simple example, converting to a MavenResolvedArtifact
MavenResolvedArtifact[] artifacts = Maven.configureResolver()
.withMavenCentralRepo(true)
.withRemoteRepo("internal-nexus", MAVEN_URL, "default")
.resolve("com.company:application:" + PROJECT_VERSION)
.withTransitivity()
.asResolvedArtifact();
My Dockerfile is also relatively simple, using openjdk9, including the bootstrapper program, a shell script and some environment variables.
FROM openjdk:9
COPY bootstrapper-shaded.jar /bootstrapper.jar
COPY docker-run.sh /run.sh
ENV CLONE_URL https://github.com/company/repository.git
ENV NEXUS_BASE https://nexus.company.com/
ENV NEXUS_REPO repository
RUN chmod +x /run.sh
RUN apt-get update && apt-get install -y git software-properties-common maven
ENTRYPOINT ["/run.sh"]
And the run.sh script copies some files (removed for brevity), runs the build, then the bootstrapper
#!/bin/sh
rm -rf /boostrap/*
cd /bootstrap/
git clone $CLONE_URL work
cd work
chmod +x ./gradlew
./gradlew clean build
NEXUS_URL=`printf $NEXUS_BASE; printf "/repository/"; printf $NEXUS_REPO` ./gradlew upload
java -jar /bootstrapper.jar 0
I expect the output to be the same as on the host machine, an array of MavenResolvedArtifacts, however the following exception is thrown on the final line of the code snippet, .asResolvedArtifact()
Exception in thread "main" java.lang.UnsupportedOperationException: No format processor for org.jboss.shrinkwrap.resolver.api.maven.MavenResolvedArtifact was found. Supported processors are: class org.jboss.shrinkwrap.resolver.impl.maven.archive.ArchiveFormatProcessor
at org.jboss.shrinkwrap.resolver.spi.format.FormatProcessors.find(FormatProcessors.java:53)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenFormatStageImpl.as(MavenFormatStageImpl.java:84)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenFormatStageImpl.asResolvedArtifact(MavenFormatStageImpl.java:71)
at org.jboss.shrinkwrap.resolver.impl.maven.MavenFormatStageImpl.asResolvedArtifact(MavenFormatStageImpl.java:40)
at com.company.ResolveTask.run(ResolveTask.java:39)
at com.company.Bootstrapper.main(Bootstrapper.java:102)
Apologies for any typos in the stacktrace, VM wouldn't let me copy-paste out of it so I had to type it out myself.
Update: Haven't found anything else on google yet, have tried clean builds to no avail.
You need to change the type of artifacts to
org.jboss.shrinkwrap.resolver.impl.maven.archive.ArchiveFormatProcessor
or at least cast it to that when you declare.

gradlew command not found?

I am working on a Java project with Gradle Wrapper (gradlew). I use Ubuntu Linux as my OS. When I run "gradle" it runs, and gives me information. But when I run "gradlew", it outputs as:
No command 'gradlew' found, did you mean:
Command 'gradle' from package 'gradle' (universe)
gradlew: command not found"
I did my research, I have the JDK, and I did sudo apt-get install gradle. How can I fix it?
The error is:
gradlew clean jpackage
Output:
bash: gradlew: command not found...
Linux and macOS
As noted in the comments, just running
./gradlew
worked for me. Adding the ./ tells it to look in the current directory since it isn't in the path.
Windows PowerShell
.\gradlew
The Gradle wrapper needs to be built. Try running gradle wrapper --gradle-version 2.13.
Remember to change 2.13 to your Gradle version number. After running this command, you should see new scripts added to your project folder. You should be able to run the wrapper with ./gradlew build to build your code.
Please refer to this guide for more information: Building Java Projects with Gradle
Running this Bash command works for me by running chmod 755 gradlew as sometimes file properties changed upon moving from one OS to another (Windows, Linux and Mac).
If you are using Mac, try giving root access to gradlew by doing:
chmod +x ./gradlew
From Mac,
Nothing is working except the following command:
chmod 777 gradlew
Then
./gradlew
The same problem occurs to me...
I check the file wrx permissions with:
$ls -l ./gradlew -> -rw-rw-r-- (no execute permission)
So I use command $chmod +x ./gradlew and this problem is solved.
In addition to Suragch's answer:
Linux and macOS
./gradlew clean
Windows PowerShell
.\gradlew clean
Windows cmd
gradlew clean
You must have the Gradle wrapper available locally before using gradlew. To construct that
gradle wrapper # --gradle-version v.xy
Optionally, pass the Gradle version explicitly. This step produces the gradlew binary. And then you should be able to
./gradlew build
For Ubuntu (Linux) users:
Doing bash ./gradlew build works, but ./gradlew build does not work.
For me, the issue was it was on the NTFS file system, and Linux does not let you execute a script from NTFS.
Try moving the code from NTFS to a Linux partition. Then ./gradlew build should work.
If you are using Visual Studio Code with Flutter, you should find it in your app folder, under the android folder:
C:\myappFolder\android
You can run this in the terminal:
./gradlew signingReport
The first thing is you need to run the gradle task that you mentioned for this wrapper. Example:
gradle wrapper
After running this command, check your directory for the gradlew and gradlew.bat files. gradlew is the shell script file, and it can be used in Linux and macOS. gradlew.bat is the batch file for the Windows OS. Then run,
./gradlew build (Linux and Mac). It will work.
Issue: Couldn't find gradlew at path jenkins
In my case, within the Jenkins CI for flutter project, I have to first run the flutter build app command, and then it automatically generated a gradlew file. And the above issue is resolved.
I put this command in my Jenkins file:
flutter build apk
In a Flutter project, don't forget to go to 'android' folder with 'cd android'.
Then you can run a command like './gradlew build' or './gradlew clean' on it (macOS).
If the answer marked as correct does not work, it is because you need to identify yourself as a super user.
sudo gradle wrapper --gradle-version 2.13
It worked for me.
I faced the same issue, but after some tries I found
it was happening because I was trying to create build in Git Bash,
instead of CMD with system administrator access.
If you create build with a command prompt, run as administrator. Then the build will get created.
If you are trying to run this command for a Flutter app, then go to the android folder first by cd android. And then use the command, and it should work.
cd android
./gradlew signingReport
I use IntelliJ IDEA and in Windows in the terminal I type:
gradlew.bat run
It is working for me.
Instead of gradlew assembleRelease, use ./gradlew assembleRelease.
I had to do dos2unix * in my current folder to make it work.
Ubuntu
Error:
Command 'gradlew' not found, did you mean:
command 'gradle' from snap gradle (7.2)
command 'gradle' from deb gradle (4.4.1-13)
See 'snap info ' for additional versions.
Use this command:
./gradlew signingReport

Java execute jar which depends on other jar from command line

I have an application that uses an external jar. I used eclipse and it works fine. I export as jar from eclipse, having created a Manifest file that has as Class-Path: ./cab.v1.jar
I place both jars in the same directory.
I run in command line:
java -jar myApp.jar
and get java.lang.NoClassDefFoundError for the classes in the cab.v1.jar (the other jar)
Have also tried java -cp . -jar myApp.jar but no success.
What am I doing wrong?
Using the documentation for the Manifest it does not use a ./ for relative directories. Try it just with:
Class-Path: cab.v1.jar
Note that the -cp option is ignored when using -jar.
If you use the -jar option the classpath is ignored. You could start the application by
java -cp jar1.jar:jar2.jar mainclass
The class path separator ':' is ';' on windows.

Categories