Maven Source Code Dependencies - maven-2

I have a couple of projects that I'm working on in Eclipse (or any IDE, so I don't want to depend on specific eclipse dependency maven capabilities). Some projects depend on other projects. Typically in Eclipse I would add the projects as references to other projects, but I'm not sure how to handle this when using Maven. In the deployment environment, there will be jars created and the maven build can pull the latest jars from the repository. Is there a way to reference another project's source code using maven while in the dev environment? For example...
I have this scenario:
Project A depends on B
I need to make a change to B and that will require an update to A. I don't want to make the change to B and check it in without checking in A since that will break A. And I would like to manage these dependencies through Maven -- any help would be appreciated.
thanks,
Jeff

The maven 2 eclipse plugin has 'workspace dependency resolution', where the dependencies are first looked up in the workspace, and then in the repository.

What I do is use SNAPSHOT version when doing my development. Project A depends on the latest SHAPSHOT version of Project B, so when I change Project B, I have Team City recompile and push out a new SHAPSHOT artifact to artifactory. Then I clean and compile project A to get the new Project B.

Related

Intellij IDEA With Multiple Gradle Projects

I am working on two Gradle projects. One of these projects is a supporting library that will be used by other projects in the future so project A depends on project B but not as a 'multi module project'. The dependency is to be resolved through the artifact repo so project a declares it as a compile dependency using it's maven coordinates.
My problem is when working on these two projects in IntelliJ changes to project B aren't made available to project A until I install it (using the Gradle Maven plugin) in my local repo. This is kind of annoying and slows down my workflow. Is there a way to get IntelliJ to automatically update the dependency internally?
This is supported in the latest versions of Gradle and IntelliJ. It is known as a Composite Build.
Composite builds can be declared in the project's settings.gradle file as follows:
includeBuild '../my-app'
or by using the --include-build command line argument:
$ gradle --include-build ../my-utils run
Take a look at the Composite Builds with Gradle and IntelliJ IDEA Webinar for instructions on how to configure the integration.
In IDEA 2017 you can right-click on the gradle module and use Composite Build Configuration to link the current module to one or more gradle module already opened in the current workspace.

How to include required jars in project while using Maven?

I am new to Maven and using it to build a project on my local. This is working nicely on my local. Now, I want to run the same project on my server and the server does not have Maven installed. So I wanted to ask if there is any way by which, when I build a Maven project on my local, I could include all the required jars in it and then simply transfer it to my server? I know Maven creates the repository in C:\Documents and Settings\username\.m2 on Windows.
But how can I include all the jars in project the way we do traditionally? I saw this question. But it talks about creating a custom repository and I don't have Maven installed at all. so I guess it is not a suitable solution to me.
Thanks.
You can use the Maven Assembly Plugin. From the documentation:
The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.

How to find the artifacts that depend on another artifact?

We are using Maven to build our projects and Nexus as repository manager.
Is there a way to know where an artifact is a dependency of another artifact?
For example, we want to know which of the artifacts in our Release repository have a dependency on commons-io 1.2, or version x.y of our framework2, or ...
mvn dependency:tree should help you get this info.
Have you looked at the Maven Site plugin in conjunction with the dependencies and dependencies-convergence report from the Maven Project Info Reports plugin?
This will generate an html report that shows all the artifacts that the project uses and what other artifacts that are dependent on that artifact.
The Maven client will only tell you the dependencies of a single project. I'd recommend using Sonar as part of your build process. It's primarily used for source code analysis, but it will also report usage of a particular library by other projects in the same Sonar database.

Maven dependancy on another non-repository project?

I have 2 completely separate projects where one depends on the other. I've very recently mavenized the main project but can mavenise the dependency if absolutely necessary.
Originally these were Netbeans projects, with the main project having several modules. What I liked is that I could declare the dependency as a dependent project. This allowed me to use the most recent code as it changes a lot (the project is in its infancy). NetBeans would put the dependent project on the classpath when running, and build a jar in the /dist directory when doing a clean and build.
Now that the main project is in maven, I can't do this anymore. The only alternative I've found was to manually copy it into the project repository, but that removes a lot of automation and ease of use. Every time I wanted to test a change I would have to rebuild the dependency, move it to the buried project repository folder, rename it appropiatly, switch back to NetBeans, then run. This is vs clicking run and everything being done automatically.
Maybe I'm just lazy, but is there an easy way to do this?
I have 2 completely separate projects where one depends on the other. I've very recently mavenized the main project but can mavenize the dependency if absolutely necessary.
Mavenizing the dependent project would help a lot. Like Eclipse or IntelliJ, NetBeans supports something Eclipse calls Workspace dependency resolution: if a project A depends on a project B and you open both of them in your IDE, A can be configured to depend on B sources instead of B jar (and any change would become immediately visible).
mavenizing the depedency project is the best option.
Alternatively you might get away with using a system scope dependency which points to the dependency project's dist/ folder jar artifact.

How do I combine library code and a maven plugin in same project?

Can I make a single maven project that can be included as a dependency (to reference Java classes inside) and executed as a plugin?
I'm working on a library to help with hosting GWT on a LAMP stack. For someone to use this, they need to extend some Java classes (so it must be a dependency) and they need to invoke a maven plugin (so it needs to be a plugin). The plugin code references the same Java classes, so if they are seperate projects, the plugin one must depend on the library one.
As is, I have the library as a normal maven project, and the plugin as a maven plugin that depends on the library. This means that to do a release, I have to release two different artifacts, and the dependent project must update both version numbers for both artifacts. It'd be nice to have a single project.
You'd be better of by doing the following
project for the jar, Foo:Foo.jar
project that uses Foo:Foo.jar as a
dependency that builds the plugin
Maven parent project that
builds 1&2
The directory structure would look like this
\project\pom.xml
\project\foo\pom.xml
\project\foo\src\main\java\foo.java
\project\plugin\pom.xml
\project\plugin\src\main\resources
\project\plugin\src\main\java
From \project you can do a mvn clean package to build \project\foo\target\foo.jar and \project\plugin\target\plugin.jar
Hope this helps.
If you create a maven plugin it still has a artifactId/groupId/version. There's no reason it can't be references both in your section and in your section. On the other hand, if thats ugly, why not just make a library with the common code that both your main project and your maven plugin project depend on?
EDIT:
Sorry, wasn't clear on the second part. Look into composite maven projects, where there is a top level pom that defines a number of child modules. In this case, the maven plugin and the common library code could be separate children producing separate artifacts, but you only need one version number and one release command executed from the top level. I haven't done this but there are any number of open source projects that do. its often used as an idiom to put testing code into a single module that can be referenced by all the others, without having it go out in any distributable jar.
The best practice is to not do what you're suggesting. Examples of this include PMD, BND, JUnit/TestNG, and so on - no serious projects seem to package the maven plugin with the library proper.
One way to get both alternatives is to use maven assemblies to have two seperate maven projects for each the library proper and the plugin and then a separate packaging as a jar containing the classes from both.