maven - how to use local repository - java

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

Related

How can we convert an existing java project with lib folder to Maven?

I have a old java project that does not use Maven or any other build tool. All the Dependencies/external jars that I need to run my project are located in a "libs" folder in my root directory. Now, i want to completely "mavenize' my project and discard the library folder.
I dont want to find all dependencies from mvn central repository and copy in the pom.xml. I have read some answers which use the install:install-file but i dont understand clearly.
Is there any fast way in which i can run my project using maven and get rid of the lib folder completely from my project?
Kindly help as im a newbie in Maven.
lib folder in java project
The way to go is what you don't want to do.
You need to look up all the dependencies on MavenCentral and copy the XML to the POM.

How do I get a local Maven package to bring in its dependencies?

I have written a series of classes that I want to turn into a company library. Managing all it's dependencies was a pain so I made a Maven project for it to be handled automatically.
I have packaged my library project into a .jar and added it to my local Maven repository. I can now list it in my application project's pom.xml, it get's brought in just like any other dependency and works great.
The problem is the dependencies of my library do not get brought in. It seems like this should be straight forward but trying to copy other packages in the Maven repository doesn't work.
Right now my jar consists of two directories:
com\company\package\Main.class
And:
META-INF\maven\com.company\package\pom.xml
Where do I need to put the pom.xml file for Maven to go get my dependency's dependencies? Is there something else in the .jar build that I am missing?
The Maven POM file (pom.xml), which describes the project/module, is usually placed in the root directory.
The POM file can then be picked up by your Maven installation, which will automatically configure the project, which means also downloading dependencies from their repositories.
For an example of an artifact deployed to a repository, see
http://central.maven.org/maven2/org/springframework/spring-core/5.1.5.RELEASE/

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.

Build Path Issue

In Eclipse we have a project where we reference an external jar in the build path. When I upload my project to the repository and my colleagues check it out, the build path looks for the jar file in the same place. One of us uses a Mac so doesn't even have a C: drive and my other colleague has a different partition containing the jar, so it always breaks.
How do we fix this issue? Ideally the jar file would be included as part of the project but it seems that the svn commit doesn't include the referenced library.
Thanks
I would suggest you use a build tool (maven, ant/ivy, gradle, etc) along with a repository manager (such as nexus or archiva), depending on what you use to build your project. These store libraries in a central location(s) and then users get the libraries from there.
In eclipse, when you include your .classpath file in the checkin, you will have the issues you are describing, since the .classpath file will contain the path to the file, and then you all must have the files in the same locations. A workaround for this is to create a "lib" directory and put all of the libraries in there. Each of the users can then add all of the libraries in that directory to their path (but do not checkin the .classpath file). This is an older way of doing things before the concept of dependency managers.
Add the jar as part of the project (say in a lib folder in your project) and commit it to svn.
Start using Maven, to resolve your dependencies.
Worst case : Commit the external jar into another project called MyProjectDependencies
You can use Apache Maven to avoid incident like this and to get many others pluses.
You can find many guides on maven, for example this one.
Eclipse has integration with maven.
1.) Check in JAR in a directory inside the project.
2.) Use Maven (or something similar) to manage your dependencies.
3.) Create a User Library referencing the JAR and refer to it this way in your project. Each user will need to create the User Library in their install of Eclipse, but it sounds like you're already doing something similar by referencing the library externally anyway.
if you don't want to use a dependency management tool like Maven, a simple solution in your case would be to use an Eclipse Classpath Variable. All projects can reference the variable, but it will have a different value for each developer.
Set up a Classpath Variable with:
Right click on the Project, select Project Properties
Click Add Variable
Configure Variables
New...
Name the variable and point to a Folder
Now commit your .classpath file. The variable will be referenced in the .classpath. Each developer can configure to their particular directory, and Extend the variable to a specific jar file.

Copy Maven dependency to webapp directory

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.

Categories