In order to follow a tutorial, I need to edit my Libraries settings. However when I right click my project and click properties, there is no Libraries category in the tree. I have done a bunch of searching and the results tell me that it just should be there by default.And I know that I want to import the dependencies to the relevant dependency folder in the tree. How can i do it?
Please what kind of project did you create? specifically the build system. The build system describes what tool NetBeans will use to manage(compiling & creating the jar file) your project.
The library menu appears when your project is based on ANT.
If the above is not helpful, kindly reply and describe the steps used in creating the project.
Dependencies to the relevant dependency folder can ad by using pom.xml. just open the website mvnrepository.com and find the groupId,artifactId and version that jar file. then add it to pom file and compile.
I want to read classes within dependency jars getting downloaded during maven build(dynamically) and write them in a file inside resources folder. Is there a way to do it? I am new to maven and I tried all possibilities to do this but couldn't find a way. Any help would be appreciated.
You can try using the maven-dependency-plugin and generating a dependency tree with an outputFile. I don't have a running example, but you can take a look of the plugin documentation here: http://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html
On the usage section you have configuration approaches that may be useful to achieve what you need.
You can also generate the dependency tree in a separate maven run with this command:
mvn dependency:tree -Doutput=/path/to/file
as is told in the usage section of the plugin documentation.
Hope this helps
I've been asked to apply Maven to a project. After browsing a dozen sites it appears that it's quite vast and I'm not familiar as I'd like with similar tools like Ant. Why is it used/preferred and what does it offer over a standard Eclipse project? Also, how could it be added to an existing project?
Why is it used/preferred and what does
it offer over a standard Eclipse
project?
It is a build tool which can build your project without the need for an IDE like Eclipse. It can create a jar or war or other artifacts from the source, performing a bunch of steps like compilation, running unit tests, etc.
Where maven scores over ant is in managing third-party dependencies and in convention over configuration (which mean less lines of build script if you follow convention).
Also, how could it be added to an
existing project?
You start by creating a new maven project, following the step here.
Place it in the root folder of your project
If your source and resource files do not follow maven folder convention, update maven properties suitably referring to this documentation.
Run mvn package
It will fail if it needs any third party dependencies, which you can add as specified in the doc
With some trial and error, you should have your project running with maven, possibly, much quicker than if you were to set up the same with ant.
Others are already provided sufficient resources to read more about maven.
I suggest to start reading here:
http://www.sonatype.com/books/mvnref-book/reference/public-book.html
Maven is a great tool when you know how to use it. Maven (at core) is a dependency manager.
You include in your pom.xml (similar in function to the build.xml from Ant) all the librairies your project depends on (example : apache commons) along with their version and Maven get them directly from a repository (by default, the central maven repository)
Then you do not have to manually install any jar to make your project work. All is downloaded and cached on your local machine. You can even create an enterprise repository where you put all the jars needed by your company
Maven uses the concept of artifacts which are pre-built library projects with their own dependencies
To mavenize a project, you'll have to write a pom.xml describing your project (examples are numerous), get rid of your libs directory (or whatever classpath you described under Eclipse) and add all your dependencies to your pom.xml
You could also check Mavenizer for a first-start
But Maven is a lot more what i've just said. Read the docs, read poms from librairies and you'll get used to it quickly ;-)
If you use the M2Eclipse plugin from Sonatype, it's just a matter of right clicking the project in the package explorer and choosing Enable Dependency Management in the Maven menu. You are also advised to adjust the directories that contain the sources to the Maven standard directory layout but if you absolutely can't, you can configure that later.
Apart from that: Well, look for tutorials and documentation (for example there is the free book Better builds with Maven. Maven is very complex (yes, I don't think it is simple) and very powerful.
Does anyone have any idea if you can find source JARs on Maven repositories?
Maven Micro-Tip: Get sources and Javadocs
When you're using Maven in an IDE you often find the need for your IDE to resolve source code and Javadocs for your library dependencies. There's an easy way to accomplish that goal.
mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc
The first command will attempt to download source code for each of the dependencies in your pom file.
The second command will attempt to download the Javadocs.
Maven is at the mercy of the library packagers here. So some of them won't have source code packaged and many of them won't have Javadocs.
In case you have a lot of dependencies it might also be a good idea to use
inclusions/exclusions to get specific artifacts, the following command
will for example only download the sources for the dependency with
a specific artifactId:
mvn dependency:sources -DincludeArtifactIds=guava
Source: http://tedwise.com/2010/01/27/maven-micro-tip-get-sources-and-javadocs/
Documentation: https://maven.apache.org/plugins/maven-dependency-plugin/sources-mojo.html
Configuring and running the maven-eclipse plugin, (for example from the command line mvn eclipse:eclipse )
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>
If a project creates a jar of the project sources and deploys it to a maven repository, then you'll find it :)
Just FYI, sources artifacts are generally created by the maven-source-plugin. This plugin can bundle the main or test sources of a project into a jar archive and, as explained in Configuring Source Plugin:
(...) The generated jar file will be named by the value of the finalName plus "-sources" if it is the main sources. Otherwise, it would be finalName plus "-test-sources" if it is the test sources.
The additional text was given to describe an artifact ("-sources" or "-test-sources" here) is called a classifier.
To declare a dependency on an artifact that uses a classifier, simply add the <classifier> element. For example:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.7.ga</version>
<classifier>sources</classifier>
</dependency>
Note that you generally don't do this, most IDEs provide support to download sources (and/or JavaDoc) from the main artifact without declaring explicitly a dependency on them.
Finally, also note that some repository search engines allow searching for artifacts using the classifier (at least Nexus does with the advanced search). See this search for example.
The maven idea plugin for IntelliJ Idea allows you to specify whether or not sources and java doc should be resolved and downloaded
mvn idea:idea -DdownloadSources=true -DdownloadJavadocs=true
To download any artifact use
mvn dependency:get -Dartifact=groupId:artifactId:version:packaging:classifier
For Groovy sources this would be
mvn dependency:get -Dartifact=org.codehaus.groovy:groovy-all:2.4.6:jar:sources
For Groovy's javadoc you would use
mvn dependency:get -Dartifact=org.codehaus.groovy:groovy-all:2.4.6:jar:javadoc
This puts the given artifact into your local Maven repository, i.e. usually $HOME/.m2/repository.
dependency:sources just downloads the project dependencies' sources, not the plugins sources nor the sources of dependencies defined inside plugins.
To download some specific source or javadoc we need to include the GroupIds - Its a comma separated value as shown below
mvn dependency:sources -DincludeGroupIds=com.jcraft,org.testng -Dclassifier=sources
Note that the classifier are not comma separated, to download the javadoc we need to run the above command one more time with the classifier as javadoc
mvn dependency:sources -DincludeGroupIds=com.jcraft,org.testng -Dclassifier=javadoc
you can find info in this related question: Get source jar files attached to Eclipse for Maven-managed dependencies
if you use the eclipse maven plugin then use 'mvn eclipse:eclipse -DdownloadSources=true'
if you're using eclipse you could also open Preferences > Maven and select Download Artifact Sources, this would let the pom.xml intact and keep your sources or java docs (if selected) just for development right at your machine location ~/.m2
In Eclipse
Right click on the pom.xml
Select Run As -> Maven generate-sources
it will generate the source by default in .m2 folder
Pre-Requisite:
Maven should be configured with Eclipse.
In eclipse - click on the project then:
.
You can, if they are uploaded. Generally they are called "frameworkname-version-source(s)"
NetBeans, Context-Click
In NetBeans 8 with a Maven-driven project, merely context-click on the jar file list item of the dependency in which you are interested. Choose Download Sources. Wait a moment and NetBeans will automatically download and install the source code, if available.
Similarly you can choose Download Javadoc to get the doc locally installed. Then you can context-click some code in the editor and choose to see the JavaDoc.
Based on watching the Maven console in Eclipse (Kepler), sources will be automatically downloaded for a Maven dependency if you attempt to open a class from said Maven dependency in the editor for which you do not have the sources downloaded already. This is handy when you don't want to grab source for all of your dependencies, but you don't know which ones you want ahead of time (and you're using Eclipse).
I ended up using #GabrielRamierez's approach, but will employ #PascalThivent's approach going forward.
If you know the groupId and aritifactId,you can generate download url like this.
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
http://central.maven.org/maven2/ch/qos/logback/logback-classic/
and you will get a page like this, chose the version you need,just enjoy it!
I have also used the eclipse plugin to get the project into the eclipse workspace.
Since I've worked on a different project I saw that it is possible to work with eclipse but without the maven-eclipse-plugin. That makes it easier to use with different environments and enables the easy use of maven over eclipse. And that without changing the pom.xml-file.
So, I recommend the approach of Gabriel Ramirez.
Maven repositories do provide simple way to download sources jar.
I will explain it using a demonstration for "spring-boot-actuator-autoconfigure".
Go to maven repository -
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-actuator-autoconfigure
The page lists various versions. Click-on to desired one, let's say, 2.1.6.RELEASE - https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-actuator-autoconfigure/2.1.6.RELEASE
The page have link "View All" next to "Files". Click it -
https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-actuator-autoconfigure/2.1.6.RELEASE/
The page lists various files including the one for sources -
https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-actuator-autoconfigure/2.1.6.RELEASE/spring-boot-actuator-autoconfigure-2.1.6.RELEASE-sources.jar
Otherwise, you can always "git clone" the repo from github, if its there and get the specific code.
As explained by others, you can use "mvn dependency:sources" command the get and generate sources jar for the dependency you are using.
Note: Some dependencies will not have sources.jar, as those contains no source code but a pom file. e.g. spring-boot-starter-actuator.
As in this case:
Starter POMs are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors.
Reference: Intro to Spring Boot Starters
In IntelliJ IDEA you can download artifact sources automatically while importing by switching on Automatically download Sources option:
Settings → Build, Execution, Deployment → Build Tools → Maven → Importing
If you want find the source jar file for any of the artifact manually, go to maven repository location for the particular artifact and in Files click on 'view All'. You can find source jar file.
For debugging you can also use a "Java Decompiler" such as: JAD and decompile source on the fly (although the generated source is never the same as the original). Then install JAD as a plugin in Eclipse or your favorite IDE.
If you're using Eclipse, I would recommend downloading both the source and the Javadocs of third-party libraries.
Right click on project and download both as per the screenshot below.
Downloading Javadocs means that typically you can get contextual help for methods from third-party libraries, with useful description of parameters, etc. This is essential if you don't know the library well. In some cases I have found that Javadocs are available when the source isn't.
Do any of the IDEs (or any other tool for that matter) have the ability to generate a POM based on an existing project?
I ended up generating the POM with a Maven archetype as Peter and Sal suggested and then moving the existing source in. Thanks for the help guys.
One way to do this it to create a template project via maven archetype then move your existing code to the template. I would recommend this only for really simple projects. It would work something like this.
mvn archetype:generate
mv src/* src/main/java
mv test/* src/test/java
mvn package
You'll get errors. To add the missing dependencies, go here: http://www.mvnrepository.com/ and start looking them up. If you pick an archetype close to what you need, you'll be half way there.
Once it all works.
svn commit -m "hacky maven port"
You can do this in IntelliJ, but the POM it generates may be more complex than if you write by hand. If your project is currently in JBuilder or Eclipse you can import this first.
Instead I would suggest you describe your project in a POM and use it to generate your project information. You can do this for eclipse and idea (and I assume other IDEs)
Your existing project probably does not use Maven, so creating a POM based on it isn't trivial.
The simplest way I can think of is to get the Maven plugin for Eclipse, generate a new maven project and then add your code.
You'll also need to manage external jars using Maven's method. In this case, they would probably be system jars.
In eclipse Galileo if you have the maven2 plugin you can select the project and then right click and go down to Maven2 in the context menu and there should be an option to convert the project to a maven project.