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
Related
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.
I have a .java file that makes use of JSON library, it imports the library's package:
However the package isn't recognised, so I tried to add the library to my project:
But it didn't work:
My library is located inside Root/libs/LibraryFolder1/LibraryFolder2/.java files and .classe files and .jar file
I tried importing both .jar and .java and library as a whole (by clicking on the root folder). Neither worked.
EDIT: I've also tried running a command:
mvn install:install-file -Dfile=C:\xampp\tomcat\webapps\Root\libs\LibraryFolder1\LibraryFolder2\Craps.jar -DgroupId=org.json -DartifactId=json -Dpackaging=jar -Dversion=20150912 -DgeneratePom=true
which gives a BUILD SUCCESS result, but doesn't seem to change anything.
Since you are using Maven as your dependency (aka libraries) management, you cannot add it in your ide and expect it to work. It will be overriten to what you have in your POM file when you synchronize the project. Moreover, if you would make this to work from IDE, compilation would fail on CLI so bye bye every auto build tools.
To make this work, you mas add your local JAR to your local Maven repository and include it trough POM file.
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
After you install your Craps.jar as Maven artifact, include it in dependencies section in POM file.
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 need to release our Maven build Java project to an remote QA team. For this I would like to download all the dependencies, and send them so they do not need to download them.
Currently all dependencies are defined in the pom.xml file, and we use either mvn install or mvn package to build the project. Some of the project members use uber jars, others use jars + dependencies to do execution.
What would be the easiest way to pre-package the dependent jar files so that there is no download from the internet, and does not change our current build process too much?
A possible solution would be to purge your local repository, tell Maven to download every dependencies and plugin dependencies of your project and make a ZIP of that.
To purge your local repository, you can simply delete the folder {user.home}/.m2/repository. Then, you can use the dependency:go-offline goal:
Goal that resolves all project dependencies, including plugins and reports and their dependencies.
mvn dependency:go-offline
This will download everything that your project depends on and will make sure that on a later build, nothing will be downloaded.
Then, you can simply make a ZIP of {user.home}/.m2/repository and send that to your Q/A team. They will need to unzip it inside their own {user.home}/.m2/repository to be able to build the project.
Offline Package deploy
Your requirement can be accomplished by creating a stand alone jar file with full dependencies. You can port it anywhere please refer https://stackoverflow.com/a/35359756/5678086
Build a full dependency JAR file as said in the answer
Copy the JAR to the destination machine you want
Run the below command from there
mvn install:install-file -Dfile=<path-to-file>
This will install the dependecies in the maven repository of the destination machine. This is fully offline
Theoretically if you know which maven commands you'll use (package, install, etc.) you could clear out your ~/.m2/repository folder, run those commands once on somebody's dev box, then distribute the repository folder. You can run maven -o install etc. to have it not give annoying warnings. This might be a slightly smaller distro than the go-offline answer.
I am in the middle of an Ant -> Maven migration project and I have a question (I'm not really a Maven expert), since I'm stuck at a particular point:
Within one of my pom.xml files I have to use the maven-antrun-plugin to call an external ANT file, which builds a jar file and puts it in a temporary folder. There is no alternative to this call. Everything is working fine - the ant script works as it should, but how can I "package" this jar in the usual Maven workflow?
I know that I could manually call the mvn install:install-file, but isn't there a possibility to configure my pom.xml in a way that the above generated jar file IS actually the artifact of that pom.xml?
you use the build helper maven plugin's attach artifact goal to attach your extra *.jar to the maven module that triggered its creation.
since having a single maven module produce more than one artifact is generally a bad idea it would be best if you isolate this in a maven module of type pom so that this would be its only artifact