Easy way to generate Maven dependencies in pom.xml? - java

I'm new to Struts and Maven, converting a Dynamic Web Project to Maven.
Under the DWP I would just drag required JAR files into WEB-INF/lib but using Maven it looks like I have to add them to pom.xml using groupId, artifactId and version.
I have no idea what these fields are for any given JAR file and have been googling around to look this up, however some simple tutorials out there use 10, 20 or 30 different jar files.
Surely, there MUST be an easier - and saner, developer-friendly - way to do this?

Those JARs are downloaded automatically from Maven Repositories, into your local repository (if they didn't exist there already).
e.g. http://mvnrepository.com/
The groupId is similar to a project prefix (e.g. javax.servlet), while the artifactId points to the JAR from that group (e.g. servlet-api). And the version is... well, the version of the JAR.
Say I want the Struts framework and API in my project. I quickly Google maven struts, and between the first results pops up this link. I pick a version, and then I look at the group and artifact id (which in this case are struts and struts).
<dependency>
<groupId>struts</groupId>
<artifactId>struts</artifactId>
<version>1.2.9</version>
</dependency>
You may find one or more implementations of the same framework, for example: from the group org.apache.struts
When your project requires a lot of JARs, then it is highly likely that some of those JAR automatically download their dependencies.
Add your struts dependency, and observe that it will automatically pull other required dependencies. In the end, you will have added about 5 JARs, tops. I can guarantee.

Related

What's a good way to bundle and add a whole bunch of JARs into a maven project?

...without specifying each and every one as a separate dependency?
So I have to work with a whole bunch of java dependencies provided by a proprietary third party software in the form of a downloadable SDK. I really want to use maven for dependency and lifecycle management, but also can't be bothered to add several dozens of JARs as individual dependencies. What's the most elegant solution to work around this issue?
Each jar must be added as a <dependency> somewhere.
So if you have just one project that uses 100 JARs as dependencies, you need to add all those dependencies (each as a <dependency>) entry.
If you have several projects using the same JARs, I would follow Conffusion's advice to create a POM with the dependencies and then use that POM in all the projects.
Maven cannot add "a directory of JARs" to the dependencies.

How to properly use any Java API

I have been trying to use the vget library/api to make my own youtube video downloader. The vget library can be found here: https://github.com/axet/vget
I have downloaded the zip on github and I imported the project into eclipse. However, I am confused to how I am supposed to properly use this API. Should I make a completely new project, and import the classes that I need or do I put my own source files in the project of the api?
I have read other threads concerning this problem. However, they all mention how a api is typically packaged in a JAR file, but in my case it is just files and classes. So I am confused to how I should properly use this api.
The vget project is a maven project. You can see that because it has a pom.xml file in the root folder of the project.
To use it, you don't even need to download the source, because the compiled jar files are already stored in the central maven repository. You can find more information about this here:
http://mvnrepository.com/artifact/com.github.axet/vget/1.1.23
(in general, you can use the http://mvnrepository.com/ site to search whether your library is available on the maven central repository. If it's even a mildly popular library, then chances are that it is)
What you need to do is to make your own project a maven project.
Here's a "5 minutes" starter guide that describes how to do that.
When you've done that, you just add the dependency on vget to your pom.xml file in the <dependencies> section:
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>vget</artifactId>
<version>1.1.23</version>
</dependency>
Since you are making use of a 3rd party software, and not extending it with your own logic, the way to go is to create a new project, which references the 3rd party software.
You then construct your application and make it do whatever you need it to do. When it comes to using logic which is available within the 3rd party logic, you would then simply delegate that call to the 3rd party library.
I have seen on the link you have provided, that this is a maven project. You have to execute a maven package command, or maven install, so that the jar file will be generated.
With this jar follow the Bill's instructions, and add it as external library to your claspath.
When you do this, you will be able to invoke methods of that api.
Let us know if you need some help doing this in eclipse.
If your project is a maven project, you can solve dependencies problems just adding the dependency written on Readme file to your pom file.
The easiest and most automatic way is to use something like maven, ant, or gradle, that will automatically download and put the jars in to your classpath if they are in the central repositories. For example, in the maven configuration file(pom.xml) you can add this to the dependency list:
VGet Maven Repository
These build tools also allow you to add external jars if needed.
If
I would suggest you get familiar with Maven. At the bottom there is a Maven dependency you just have to include into your pom.xml, and then you can use the extension immediately.
Maven is a build platform which organizes your project in a technical way (convention over configuration, e.g. code is in /src/main/java, tests are in /src/test/java). The proper way is it to create a Maven project in Eclipse (you have to install the plugin and download Maven as well) and put the dependency
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>vget</artifactId>
<version>1.1.23</version>
</dependency>
into your <dependencies> inside your pom.xml. After adding it, you project recognizes the additional package automatically.
Nobody tinkers by adding libraries manually. It's actually not professional to work without a build platform like Maven or Gradle.

What is a Maven Dependency exactly?

I have just started to learn Spring using Maven. Can somebody clearly explain?
In your codebase, you will have a multitude of packages. Each of those packages will have a pom.xml file which have maven dependencies in there. Those are the dependencies that get pulled in when doing an 'mvn install' on that particular package. E.g. one of your packages which uses spring will probably have this :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
Further, your package will also depend on other packages, and so will have dependencies on those packages. Each package gets a .jar file of its own when built (which contains .class files). A certain package doesn't rely on all other packages in the codebase, so it just pulls in the ones needed. These packages can be published and pulled in from a locally hosted Artifactory, and in the case of spring it probably gets pulled in from an online maven repo.
The fetched artifacts (.jar files) get put into a hidden repository folder (mine's called .m2/repository) which you can configure in your IDE, and the fetching is done smartly. If it exists already, it won't do the effort to pull in a new one. If you however do want to override the currently fetched artifact, look at this question I asked when I was struggling to understand maven myself.
Notice the < version > tag. This tells maven the version to fetch, and if it sees that version already exists (I'm not sure how it checks, it probably looks at the folder name or some file inside like MANIFEST.MF) it doesn't bother fetching it. In case you have a dependency which has frequent updates, changing this version field all the time can be bothersome, you can make it such that it fetches the latest all the time.
Hope that helps.

Dependencies in pom file - how does it work

I see some java programming examples on the internet which require me to use non-standard api's. So, the authors mentioned that a pom file is needed. I need to understand what is the purpose of this file and how does it get bound/"binded" to a java project.
When I add a non-standard jar to my project's build path in eclipse, is it the same as creating a pom file, doing something "magical" with it and then "binding" it to my project?
Or is it something else ?
The pom file is the main artifact of Maven, which is a build tool. Maven has many uses, but the most common is dependency management (build management is also a core feature). The way it works is that it allows you to define dependencies to external libraries that your project needs, and when you use Maven to build your project it will fetch these libraries from the web (external repositories) and add them to your built project. So it's an automatic handling of dependencies.
So if you for instance you want to include Spring in your Maven project, you edit your pom.xml and add
<dependency>
<groupId>org.springframework</group>
<artifactId>spring-core</artifactId>
<version>3.1.3.Final</version>
</dependency>
Where the groupId and artifactId is an unique identifier of the library you want to include, and version to select the right jar-file. Now, when the project is built with Maven (mvn install), it will try to fetch jar from the web and add it to the classpath of your built project.
Maven might be a bit hard to get started with, since there are so many options, but for learning Maven I would recommend starting with the build cycle and dependency management, and then move on to more advanced topics when you have a good understanding on how these things work.

Maven and AppServer dependency management best practice

I am developing a web-app and use maven for dependency management (duh). Some of the needed jars are already available in the server lib folder, but do not match the "maven naming scheme", ie missing the version suffix.
I would like to use them for development and deployment, but..
1. i cant point maven to them because maven seem to need a version suffix. I cant omit it in the pom.
2. If i define the dependency outside maven then maven is obviously unable to build.
3. Renaming the files inside the server distribution sounds like a kludge.
What would Brian Boitano do? I mean, there sure is an elegant solution that im not aware of, or at least a good argument for one of the three solutions above.
Thank you
PS. i am using jboss 5.1 and maven 2.2.1 atm, but its subject to change
You can provide those jars as a dependency with a system scope if you want explicitly to identify where they live. For more info have a look here
IF those are not proprietary libs you are using, I'd recommend you use official versions of those from maven repository.
If they are proprietary you can manually install jar to your local repository using maven(you can use your version, suffixes, group names, artifactid etc) and then use them in your pom.

Categories

Resources