After running Maven - clean, I see sub folders in my M2_REPO folder. junit.jar was created in M2_REPO/junit/junit/4.11/ alone with some other files. Because of this sub folder structure, eclipse is not able to recognize the junit.jar file. What am I missing?
You're looking in the wrong directory. Eclipse shouldn't read the jars directly from this directory, it is the local cache of what maven manages.
The pom.xml is where you should put your dependencies, and eclipse will then use the maven libraries to get the required jar file, put it on the build path, and possibly cache it in the directory you are looking at.
Note that if you want the junit jar file for testing purposes, the typical file in src/main/java will not see it, because testing source code goes in src/test/java.
This means that maven provides (through the libraries) (at least) two classpaths. One for typical compilation and one for test compilation and execution. Adding in a <dependency> tag section puts it on the compilation and run paths; but, to make it "test only" you need a nested <scope> tag.
Maven correctly is creating those directories the first time it tries to acquire those artifacts.
For integrating maven and eclipse, try running mvn eclipse:eclipse
This should enable eclipse to see the jars maven has downloaded.
That is the path of the local M2 repository and the sub-folder structure is exactly how it should look.
Make sure that you import the project in eclipse as a maven project by doing
File -> Import -> Maven -> Existing maven project
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 cloned a GitHub repo onto my disk an successfully built the project by:
ant clean-all resolve create-dot-classpath
within the projects root directory.
I assume this should also be possible from IntelliJ IDE but that fails by doing nothing at all - it simply states "All files are up-to-date" ... what I do is:
"Make module 'pentaho-kettle'"
"Compile module 'pentaho-kettle'"
I could use a hint where to look for a solution or what configuration is most likely missing.
As the project is a multi module project based on Ant and Ivy, you have to
import the project from sources
configure modules (like core, ui, the plugins, …) with the right directories for main and test sources and resources (they seem to overlap with java sources in that project)
setup the class path using an Ivy plugin (I recommend IvyIDEA), use the context menu on the ivy.xml file
and have luck to build. It will be some try and error!
You may also have a try importing from Eclipse as those .project and .classpath files are checked in. But than you also have to check the module directories, the import is more a partial guess and I worry that IntelliJ will not get any information about Ivy from the Eclipse setup.
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
I have two projects in Eclipse, the first project depends on maven, the second project which dependent on the first one does NOT depend on maven.
The first project downloads external libraries like jar files and natives to the .m2 maven folder. However the second project gives a ClassNotFoundException since it cant find the jar files and the native files from the first project.
Is it possible to link these downloaded jars+dlls with the second project without having to reference in the build path->libraries in the second project properties?
I would appreciate any help.
In your Maven project, use Assembly plugin to create an Uber-jar that contains the project build artifact and all its dependencies (mvn assembly:assembly -DdescriptorId=jar-with-dependencies). Then, reference that from project #2, either with a relative path or by using an ant build task to copy it into your other project's lib directory (assuming you have such a directory). Also, although it's frowned upon, you could configure the assembly plugin so that your Uber jar artifact always has the same finalName.
Is it possible to link these downloaded jars+dlls with the second project without having to reference in the build path->libraries in the second project properties?
I don't think so.
But maybe you could create a 3rd project (which is a Maven project) that depends on the first one, and on the JAR (or whatever) file created by the 2nd one as a non-repository dependency.
Having said that, anything you do is going to be a bit of a hack. You'd be better of either turning the 2nd project into a proper Maven project, or creating a custom build script that manually pulls the 2nd project's dependencies from somewhere. (I think that Ivy could help you with that ... assuming you use Ant in the 2nd project.)
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.