how does a Java program find its Maven packages? - java

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.

Related

Java Maven -> quickls add folder of jars as depencency

my Java Project uses a "/libs" folder containing ~100 .jar files. Almost all of them are not in an official maven repository.
1.) In the moment I manually added to whole folder to the classpath with my Eclipse IDE. That enables to compile and run the App using the Eclipse IDE. But if I want to maven to compile and create jar-with-dependencies, maven of course does not know about the "/libs" folder.
2.) I know that I can add a jar file to my local maven repo with mvn install:install-file but this would take a very long time because I would also have to open every jar and find the whole package name to insert as '-DgroupId' and the Name of the Main Class to add as '-DartifactId'
3.) My Questions:
3.1) Is there an easy way to let maven just include all jars in a folder like I did with my Eclipse IDE? I know that would break the principle of maven that every jar is identified with group and artifact id, but it would be a quick solution.
3.2) If it is not possible to add a folder with jars as a dependency in maven, is there a faster way to add a jar file into a local repo. It would be easier if there is a maven command where groupId and artifactId are automatically discovered by the jar that I do not have to open every jar file and find the Main Class and its classpath
Quick answer: No.
In the past, I have written a script for that because there is not support in Maven for this.

Export jar Eclipse Maven Project with just non-provided dependencies

I'm building a Java project using the Maven Project package in Eclipse Java EE IDE. I'm using different dependencies, some of them will be provided by the system where the java program will be run on, others will not. I added the provided scope tag into the pom file to the one I know are provided by the system and I now I would like to export a runnable .jar. Eclipse exports the .jar package with all the dependencies (provided and not) but there's a way to have the runnable .jar file with just the not provided dependencies packaged?
If you exporting a jar with eclipse, eclipse Jar-Packager will be used and the runnable jar will contain all dependencies. Eclipse jar-builder don't know about pom.xml.
In your case you should use mvn build, for example:
mvn clean package

Executing a JAR file straight from a Maven repository

Suppose that we have a Java application, rather than a library, which is available through Maven central. For example, one such project is jol, which has its corresponding CLI interface in Maven central.
As far as I can tell, the main difference from a library is that the corresponding JAR file contains a class with an appropriate main() method and, optionally, a related Main-Class: header in the JAR manifest. If such an artifact is used as a dependency on a project, Maven will happily download the JAR file to the local repository along with its dependencies, as it does for any other artifact.
Is it possible to use Maven to execute such an application directly, without setting up a Maven project?
The exec:java plugin works nicely for local projects by setting up the JVM classpath so that dependencies are available. The user does not have to worry about JAR or .class file locations and such. Unfortunately, from what I can tell, it also requires an enclosing Maven project, so it cannot be used from an arbitrary command line prompt.
No, Maven will not do what you are asking for. It is a build tool, intended to build a Java project based on it's pom.xml file which describes the project.
So, you can't run a maven build without a pom.xml file.
And if you have a pom.xml, then by definition, you have 'set up a Maven project'.
As #DaveNewton says, you should be able to set up a very small pom.xml with the dependency for the jar file in question, and the exec-maven plugin. I'm afraid that it's just not going to get any simpler than that.
A hacky solution working with Maven 3 would be to use the Maven Dependency Plugin in combination with the Maven Help Plugin to resolve the local repository path:
# Download JAR from Maven repo
mvn dependency:get -DremoteRepositories=http://repo1.maven.org/maven2/ \
-DgroupId=some.group.id \
-DartifactId=some-artifact-cli \
-Dversion=1.0.0 \
-Dtransitive=false
# Resolve local repository path
MVN_REPO=$(mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout)
# Execute JAR from the local repository
java -jar $MVN_REPO/some/group/id/some-artifact-cli/1.0.0/some-artifact-cli-1.0.0.jar

How to add a jar package to project in eclipse?

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.

Maven: How to generate a standalone application?

I am using Maven to manage a console application project. On my machine, I type mvn exec:java and Maven handles everything. What I want is, however, to execute the same application on a different machine without the help of Maven.
In NetBeans, Ant projects have a dist directory with all the necessary files. All you have to do is to type java -jar dist/App.jar. How can I make Maven generate such distributable directory or archive?
PS: Although seems relevant, this is not a duplicate of Create a standalone application with Maven.
I have used in maven.
http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/
The Application Assembler Plugin is a Maven plugin for generating scripts for starting java applications. All dependencies and the artifact of the project itself are placed in a generated Maven repository in a defined assemble directory. All artifacts (dependencies + the artifact from the project) are added to the classpath in the generated bin scripts.
and http://maven.apache.org/plugins/maven-assembly-plugin/
The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.
You can build an executable jar file with the maven-jar-plugin; more info on their examples page here: http://maven.apache.org/shared/maven-archiver/examples/classpath.html
That will simply create an all-in-one jar that can be executed through java -jar

Categories