Copy Maven dependency to webapp directory - java

this is similar to this question but with a twist.
I have a jar in a remote maven repository. This jar is actually a applet that gets run in the web browser. So it needs to get copied into webapp/resources/ directory.
I'm trying to use the maven-war-plugin to copy it and I'm so close but I can't seem to get the syntax quite right since the jar is just in the local m2 directory and not really inside the build directory at all (although I believe it is in the resulting war.)

The dependency plugin can copy artifacts by maven coordinate from a repository (local or remote) to arbitrary directories. Just add an execution of it somewhere before your war is built to copy the jar to an appropriate place.

Try the copy task of the maven resources plugin. This will let you call out your jar just like you do a dependency, but it will copy it wherever you like so that it can be included in your war.

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.

Maven creates sub folders in M2_REPO Directory

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

maven - how to use local repository

I am newbie to maven. I have few questions on it.
I have to build a webapp and prepare a war out of it. I have put my web application under ..\src\main\webapp. And all classes of it, which were under web-inf\classes, i have copied to ..\src\main\java. And i have put pom.xml in the root directory.
I have numerous jars in web-inf\lib folder, on which my compilation of classes depends on. I want to add all those to my local repository. My local repository is default folder, which i have not changed in settings.xml. So, how do i put all these jars to my local repository? Just copying jars to local repository folder is not working.
Should i create a folder for each jar as per its groupid? Am i doing things correctly? Or i am missing something?
Kindly help. Thanks in advance!
To deploy to maven local repository you can use an install plugin:
http://maven.apache.org/plugins/maven-install-plugin/examples/specific-local-repo.html
Your question is confused enough. Firstly try to explain in a better way how to are doing?
If you are trying to add maven nature at your project (like I think) there are something to review.
I'm not able to understand when you say:
And all classes of it, which were under web-inf\classes, i have copied to ..\src\main\java
Usually in web-inf/classes there are the .class files, while in src/main/java there must be the source files. And this is an strange thing
Finally fon dependencies copy you have to just configure maven correctly and add all jar like project's dependencies. Then when you try to install you application maven will download the jars file in your folder

Giving the war from local system rather than from Maven repo

I'm trying to manipulate an open source project, where I have made my changes and created the WAR file. The WAR file that I have created is an "dependency" for an another module.
The code for that "dependent" module uses org.eclipse.aether.artifact.DefaultArtifact like this:
public static DefaultArtifact getReportArtifact(String version) {
return new DefaultArtifact(ALLURE_GROUP_ID, ALLURE_REPORT_FACE_ARTIFACT_ID,"WAR", version);
}
to get the WAR and builds it jar.
I need to pass the my local WAR file, instead of it getting from the Maven repo. How can I do that?
Thanks in advance.
If I understand you correctly:
Maven: The Complete Reference, 3.4.1. Dependency Scope
system
[...] you have to provide an explicit path to the JAR on the local file system. [...] The artifact is assumed to always be available and is not looked up in a repository.
There are multiple approaches you can take. The most portable would be to specify an additional Maven repo in your project at a path inside (or relative to) ${project.basedir}. In there, you could install the artifacts you need, in the normal Maven fashion. This way, the build would be portable across environments without modifications.
But, I must say, I only half-understood the question as you never really explain what you're trying to achieve.

Link maven dependencies on non maven project

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.)

Categories