Why don't we build jar file in a dockerfile [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 27 days ago.
This post was edited and submitted for review 26 days ago.
Improve this question
I have a spring-boot application. I checked a couple dockerfile example for sprint-boot application. No body does not build jar file in dockerfile but it is possible. Is there a unexpected thing that I missed.
I dont even see multistage builds in spring boot official docs
This is my Dockerfile, as you see I am using multistage build here. This is working fine
FROM maven:3-openjdk-18
COPY . .
RUN mvn clean package
FROM openjdk:19-alpine
VOLUME /tmp
COPY --from=0 target/*.jar app.jar
CMD ["java","-jar","/app.jar"]
My question is, Is this OK to use like this or Is there any important thing that I miss

One advantage of a Spring Boot executable jar is that it contains all of your application's code as well as its dependencies. This gives you a single unit that you can deploy. When you're packaging your application in a container, it becomes this single unit of deployment with the added benefit that it also contains the JVM and anything else your application needs. Given that the container is a single unit of deployment, the jar packaging arguably isn't needed any more.
As described in the Spring Boot reference documentation, unpacking the executable jar when building the container can result in a slight improvement in startup time. You can explode the jar file and then use Spring Boot's JarLauncher to launch the exploded "jar":
$ jar -xf myapp.jar
$ java org.springframework.boot.loader.JarLauncher
This approach ensures that the classpath has the same order as it would have when running the application using java -jar.
Alternatively, you can specify the classpath manually and run the application's main method directly:
$ jar -xf myapp.jar
$ java -cp BOOT-INF/classes:BOOT-INF/lib/* com.example.MyApplication
This will result in a further small boost in startup time at the cost of potentially changing the order of the classpath.

Related

How can I run Jsp project without eclipse [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a JSP project.It was created using Eclipse.
Is it possible to run a Eclipse project(without Eclipse).
If yes, then how can I convert its folder structure. So I could run it on my Apache Tomcat Webserver(without IDE).
I downloaded this project from internet. It was created using Eclipse.
My system specification is
J.D.K. 8
Apache Tomcat 8.5
You can reproduce this problem by using
This J.S.P. project which I have dowloaded.
https://drive.google.com/file/d/1uZT2D0CljfnWWaC_fchFbgQcjCcKiUZI/view?usp=sharing
I have read following post:
How to run a J.S.P. program
This post suggest that I should create a war file. But I could not find any tutorial to do it without eclipse.
Tomcat or any other server does not know (and is not supposed to know) which IDE was used to create the web project. If you have .war file, just deploy it in Tomcat and it will run without making any change.
The command to create a .war file is as follows:
jar -cvf abc.war *
where abc can be any name of your choice.
Copy your project folder to a new directory. Go to that directory using cd command and then use the command given above to generate the .war file.
Also check How to deploy a war file in Tomcat 7 to understand how to deploy the .war file.
To run your project with a webserver like Tomcat, you need to build it first then deploy it to the webserver.
From Eclipse right-click on the project then export as war.
then deploy this war file to Tomcat.

Standalone/Embedded Web Server to make a portable Web app [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I come across the following options to make a portable web application in java
Spring Boot with embedded tomcat - Not sure how to build for windows, linux and mac
Jetty embedded server - http://www.eclipse.org/jetty/documentation/current/embedding-jetty.html
Tiny - https://github.com/NanoHttpd/nanohttpd
JLHttp - https://www.freeutils.net/source/jlhttp/#whatsnew
Undertow - https://www.stubbornjava.com/guides/embedded-java-web-server and http://undertow.io/blog/index.html
Not sure which one is the best to go..Please advise.
Thanks.
I think the first choice(Spring Boot with embedded tomcat) is easy according to my experience. I have used the Spring boot embedded tomcat for the deployment of both Linux and Windows. I will explain how to deploy using embedded tomcat. Still then, the decision is yours to choose.
Ubuntu
1. Create executable jar/war file: You can create an executable war/jar by adding the following configuration in your pom.xml file or you can change the permission of the jar/war file using the Linux commands.
Add the following configuration in your plugin session of the pom.xml file as follows:-
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
Then open the terminal and navigate inside the project folder and run the following command:-
mvn clean install
As soon as you run the above command, you will get an executable war/jar(as you mentioned in the pom.xml) /target folder inside the project.
(or)
You can skip adding the above configuration and use Linux commands to make the war/jar as executable one. To do so, just open the terminal and navigate to the project folder. Then run the following command:
mvn clean install
You will get the war/jar in the /target folder which is not executable. You can make it executable using the following Linux commands:-
chmod 777 file_name
As soon as you get the executable jar/war, then you are ready to deploy it as a Linux service now. Because when you run it as a service, you don't need to restart it time after time whenever the server gets restarted. By default, the spring boot jar/war file has the ability to support init.d service commands like(status, start, stop, restart). To run it as a Linux init.d service, use the following steps:-
Copy your jar/war file to the /var folder(No problem even if you keep the jar/war in some other locations) using the following command.
cp file_absolute_path destination_path
e.g cp /opt/test/test.jar /var/test/
then create a symbolic link to the jar or war file as follows in /etc/init.d folder using the following command.
ln -s file_path symbolic_link_path
e.g ln -s /var/test/test.jar /etc/init.d/test
So the service has been created now. You could check the status of the service using the following command:-
sudo service service_name status
e.g sudo service test status
if you get an error like Unit test.service not found, then execute the following command to enable the service.
sudo systemctl enable service_name
e.g sudo systemctl enable test
and then execute the status command, you will get the status of the service.
In a similar way, you can use other commands also to start, stop and restart the service.
sudo service service_name start/stop/restart
Windows
Even though there are more ways, I would like to explain about running the tomcat as a service which is quite simple. Use to following steps to do so:-
Disable the embedded tomcat inside the spring boot application by adding the following command in application.properties
spring.main.web-environment=false
(or) add the following configuration in your spring boot application main class:-
#SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,
WebMvcAutoConfiguration.class})
Then build the war file using the Maven command mentioned above:
mvn clean install
Copy the war file from the /target folder.
Download a tomcat 7 or above version for windows from the official site tomcat.org
Copy your war file and paste it to the tomcat/webapp folder.
Open Command prompt and navigate to the tomcat's /bin folder
Execute the service.bat as follows to create a service.
service.bat install service_name
Open Services manager and you could see a service running with the service_name as you created.
when you select the service, you could see the options like start/stop/restart on the left side.
You can also make the service to run automatically whenever the system gets restarted. Select service -> Right Click -> Properties -> Startup Type -> Select Automatic
If you talk about java, Spring is the hottest and most demanding framework of all. Undoubtedly go for option 1. Few reasons to use Spring are as below
Less coding, more productivity
Integration with database simpler using Spring JPA
Template made easy by Spring Thymeleaf
Rest calls made concise with Spring Rest Template
Form validation modularized by Spring Validator.
Secure application using Spring Security
Define workflows using Spring State Machine
Numerous articles and example available over internet
Community and developer support easily available
.. and many more

Default micronaut application not running

Not being able to execute the default micronaut application using basic cli commands. This problem has occured on both the Maven and Gradle default projects. I don't understand why I'm having issues running the app.
The repective default project base directories contain the mvnw and gradlew files so I don't think it's a problem with maven/gradle being out of date on my computer or anything along those lines.
I've checked the class path being passed into the arguments in both the gradlew and mvnw bat and cmd files. Their respective wrapper.jar paths are being passed in as classpath arguments.
Here's some images.
Maven error message, similar error occurs when trying to run with gradle:
Base directory of maven project:
Gradle error message:
Base directory of gradle project:
Parent directory image:
UPDATE: I just tried downloading someone else's project directory from a guide on micronauts website I unzipped it and used the ./gradlew run command, the server seems to be up and running. The zip comes with initial and completed subdirectories each with their own micronaut application inside, they run too using the same gradle command.
This leads me to believe that there was something wrong with my micronaut installation. Maybe it's shipping a faulty default application (that's not configured properly)?
It looks like you don't have the wrapper support files in your project.
~ $ mn create-app somedemoapp
| Generating Java project...
| Application created at /Users/jeffbrown/somedemoapp
~ $ cd somedemoapp/
somedemoapp $ rm -rf gradle
somedemoapp $ ./gradlew run
Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain
(notice that I deleted the gradle/ folder before attempting to run the project)
Some people don't check in the gradle/ directory to source code control because they don't understand its purpose.
I created a new micronaut application in my C drive with a different directory, it worked. I think it has something to do with the original path variable having too many spaces and - in it. Making the classpath (in the gradlew.bat) unreachable essentially.

What are auto executable jars?

I was going through spring-boot-maven-plugin documentation and came across a term auto executable jar.
Could someone please explain me what is an auto executable jar and how is it different then normal jar files and how they are auto executed?
spring-boot-maven-plugin documentation mentions the term but does not go further to explain it
repackage: create a jar or war file that is auto-executable. It can replace the regular artifact or can be attached to the build lifecycle with a separate classifier.
Could someone please explain me what is an auto executable jar
A fully executable jar can be executed like any other executable
binary or it can be registered with init.d or systemd. This makes it
very easy to install and manage Spring Boot applications in common
production environments.
So In conclusion is like any other executable when you use a executable jar
how is it different then normal jar files and how they are auto executed?
Well a java file you need to run with java -jar
From Spring Docs
The Maven build of a Springboot application first build your own application and pack it into a JAR file.
In the second stage (repackage) it will wrap that jar with all the jar files from the dependency tree into a new wrapper jar archive. It will also generate a Manifest file where is defined what's the application Main class is (also in the wrapper jar).
After mvn package you can also see 2 jar files in your target directory. The original file and the wrapped jar file.
You can start a Springboot application with a simple command like:
java -jar my-springboot-app.jar
I may suggest that auto executable means that you supplied main method so that it can be launched with java -jar options, otherwise it may be just a java library.
Here is a quote from https://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html
Repackages existing JAR and WAR archives so that they can be executed from the command line using java -jar. With layout=NONE can also be used simply to package a JAR with nested dependencies (and no main class, so not executable).
Executable jar - the one that has main class declared in manifest and can be run with java -jar yourJarFile.jar command
Other jars - jars jars without delcared main calss. Can be anything - application, library, etc. Still can run application by providing fully.qualified.class.name as entry point like java -cp yourJarFile.jar my.bootstrap.BootstrapClass
Autoexecutable jars - never heard about it :)

Running Standalone Jar need to add jar on server to run

I created a spring boot job which relies on properties on the server and I can get it to run like so, no modifying manifest.
/bin/java -Dspring.config.location=/var/tmp/com.jdbc.properties -jar my.jar
and it works. But the application relies upon another jar that is an internal jar that lives under /usr/local/share/jni/foo.jar which I want to add to this mix.
I have tried countless runs trying such things as:
java -cp /usr/local/share/jni/foo.jar -Dspring(picking up original line)
When I start to google this, it takes me on magical tours of running:
'org.springframework.boot.loader.JarLauncher'
or
'org.springframework.boot.loader.PropertiesLauncher'
then mucking with manifest etc.
Spent last 4 hours with no success. Is there a best practice to run a standalone jar that needs to consume remote properties file and an additional jar file? Would like to keep it simple if possible.
If you are using Spring Boot and want to have a Fat-jar that encapsulates all your dependencies, the best way is to add the required Jar as a dependency to your project.
Assuming you are using Maven to build your project, the "foo.jar" needs to be added as a Maven dependency to your project. Then, spring Boot maven plugin will pick up the jar and includes it in your Fat-jar.
Even if the "foo.jar" does not exist in any Maven repo, you still can add it manually to your local Maven repo using the Maven command mvn install:install-file (See Maven doc).
Did you try using foo.jar as a provided dependency within your maven/gradle dependencies and building the project as executable war file?
See spring boot's maven plugin description of building executable war files.
Overall. Run spring boot standalone jar on a Linux server. Additionally read the database properties from a static file on the server, and path in a jar file that adds functionality that only lives on the server. Cannot include in the boot lib.
command line run (will convert to shell) and ran.
/path/to/..openjdk-1.7.0.55.x86_64/bin/java -cp /usr/somewhere/jni/Foo.jar:/path/where/lib/MYBOOTJAR.jar org.springframework.boot.loader.JarLauncher --spring.config.location=/path/to/properties/on/server/com.xxx.yyy.zzz.jdbc.properties
Seems like using the JarLauncher (no modifications to manifest, except excluding the Foo.jar from local)
Hope this helps someone else.

Categories

Resources