I need to export executable jar from intellij spring boot project. its basic rest only. How can it possible. I have created the artifact jar and when i try to run it with java -jar xxx.jar it returns manifest file not found error.
Generally you have this error when you generate the JAR using the IDE directly (export option of Eclipse for example).
You can either use the Maven goals clean install or clean package to compile and generate the JAR in your target folder and/or local repository.
package - take the compiled code and package it in its distributable
format, such as a JAR.
install - install the package into the local repository, for use as a
dependency in other projects locally
The install will execute the package one step before install it on your local repository.
Maven Lifecycle Documentation
If you have installed maven, On the terminal of Intellij Idea use following code.
mvn clean package
Your Jar will be saved inside "target" folder inside the project directory
Related
How to export a java maven file from eclipse with all external libraries and maven dependacies so the other peorson opening does not have to download and import the external libraries?
I tried the Archive file exporting method. But it wasn't successful, cause it lost the external libraries while importing, and it lost clases and packages .
The default local repository is located based on OS:
Windows: C:\Users<User_Name>.m2
Linux: /home/<User_Name>/.m2
Mac: /Users/<user_name>/.m2
Assuming that you want to store these dependencies in the project folder so that every user does not have to download and import the external libraries according to this source you can download all your dependencies to the target folder with the following command.
mvn install dependency:copy-dependencies
You can also specify the folder.
mvn dependency:copy-dependencies -DoutputDirectory=specific-folder/
To make use of these commands you will have to install Apache Maven Dependency Plugin
So I need to connect a library to my repo in IntelliJ, so I can test the library as it interacts with my main project. And I got this command from a member on my team: "So you need to change .java files in entity manager (which is the library) repo and do mvn clean package to generate the jar file locally, the use that jar in your project repo.
Then create dummy folder in your project with the same package name and copy those .java files from entity manager."
What does change the java files mean? What about "use that jar"? I'm so confused.
Use "mvn install" instead of package. This puts the library into your local .m2 repo and makes it available to your project in idea
If you do mvn clean package it produces a .jar file in target/ folder. Use that dependency to your project if they both share a common parent pom.xml
If you do mvn clean install it produces the jar file in target/ folder + copies the .jar file to your local m2 folder. That way you can use that .jar file to any project locally. Just use the with matching version, artifact and groupId whereever you need to use that jar file.
For both options above, you need to define sth like below in your pom.xml
<dependency>
<groupId>com.my.library</groupId>
<artifactId>mylib-artifact</artifactId>
<version>1.VERSION</version>
</dependency>
Not recommended:
You can also put the .jar file to your project (without doing mvn clean install) See: http://blog.gtiwari333.com/2016/12/maven-use-local-jar-without-installing.html
Or manually add the .jar to your project as project dependency using your IDE's project configuration
I have a Java program in IntelliJ which has a pom.xml and uses Maven. The packages were downloaded and currently they are found by IntelliJ.
I'm a little confused though because the Maven repository is not part of the CLASSPATH as far as I can tell. So does IntelliJ just do a bit of magic where it looks into its Maven repository to find the packages? (I think that IntelliJ has its own Maven repo. I separately have Maven 3 installed, but I think it isn't using it.)
But more generally: If you build a JAR using Maven then I guess it will put the dependencies in the JAR where the Java program can find them, so there won't be a problem. But if you just run a Java program directly, do you need to add the Maven repository to your classpath or does something else happen?
Thanks for any information you can provide to lessen my confusion :)
When you start the program from IntelliJ using a runtime configuration for your main() method IntelliJ constructs the classpath from all the project dependencies. You can see this in the Run window, the first log line is the java command used to start the main(). It's a long line but it usually looks similar to:
java -javaagent:/opt/idea/idea-IC-173.3727.127/lib/idea_rt.jar=40165:/opt/infra/idea/idea-IC-173.3727.127/bin -Dfile.encoding=UTF-8 -classpath /home/ [...]
IntelliJ constructs the -classpath argument adding both the module target directory and the Maven dependencies referenced from the local Maven repository.
When you package the project using Maven mvn clean package usually it becomes a standalone JAR with just your code (unless you changed the defaults). Now you have a few choices how to provide dependencies needed to start your main():
Provide them using -classpath parameter just like IntelliJ.
Add maven-shade-plugin and use shade goal to the build a runnable Uber JAR. This creates a fat JAR which doesn't require -classpath.
Use some other Maven plugin to perform point 2 e.g. Spring Boot spring-boot:repackage goal.
All the required dependencies, defined in the pom.xml file(s), are downloaded from Maven Central (or others if configured) to the local Maven repository. That repository is located at <user home>/.m2/repository.
Maven generates/calculates a dependency tree to know all the required dependencies for the project. (you can also dump that tree with the command mvn dependency:tree. I always pipe the result to a file, because the tree can be large mvn dependency:tree > deptree.txt). Maven put them all on the classpath when executing a maven command like mvn compile
IntelliJ also use/calculate the dependency tree and add all the jar files to the projects classpath (point to the files in the <user home>/.m2/repository folder). You can see them all in the list with External Libraries, and they will be used / on the classpath for compilation and running the application.
When building a JAR file the dependencies are NOT added to the JAR. Only the bytecode (java classes) and resources from your own project are packaged into the JAR file. (Source files can also be packaged if you configure that)
By adding a Maven plugin (maven-shade-plugin) you can configure your project to also pack dependencies into the JAR. SpringBoot projects also will do that.
I have added to my pom.xml a section that specifies the mainClass and allows it to essentially create an executable jar. I have included a bunch of dependencies that maven manages as well. It executes fine, but fails to run when it gets to a section of code that needs to know the location of a jar package that was made inhouse by somebody (i.e., not from Maven). In the project in eclipse I had put the jar in src/lib and my code is in src/main/java. I had to select properties and Java Build Path and specify there the src/lib location for the jar to get it to even compile. However, trying to run java -jar name.jar has it fail and complain because it fails to import the classes from the src/lib jar. Since it is not a maven thing, how to I make sure this is a dependency for this project and that it is seen on the project's classpath?
The thing with maven is that maven has to control all of the dependencies and that includes this jar you want to reference. That doesn't mean that you have to build that other jar using maven, you could mvn install it in your local repository or use a tool like Artifactory to put it in a private remote repository. I know that installing regular jars via Artifactory creates a pom file for the jar and from then on you can treat the jar like any other maven dependency.
I want to build my project with Maven. How do I do that?
Maven is installed,
the project is called Sertsu1
it contains a pom.xml-file
What must be entered in the command line to start building?
If your project is organized as Maven expects, e.g. your source code is in the src\main\java directory you can run
mvn package
to just build your jar
mvn install
to install it in your local Maven repository
mvn clean
to remove a previous build
Beware that you won't go very far with Maven without reading about it. You can start with this book.
In order to create an artifact (jar-file) you need to invoke
mvn package
This is very basic and you should take your time to read the suggested manuals before using maven.
Navigate to the folder that contains the pom.xml and enter
mvn package
(for a quick result)
Your machine needs to be connected to the internet as maven will download a lot of files from public repositories.