maven

Maven in-project repository

Posted on

source: http://blog.dub.podval.org/2010/01/maven-in-project-repository.html

Why in-project dependencies?

Maven handles dependencies of the project very well. It finds the artifacts in the configured repositories, retrieves their POMs and resolves transitive dependencies.

In some cases, artifacts are not available in well-known public repositories. For instance:

  • artifact is proprietary
  • nobody bothered packaging the artifact (e.g., Saxon9)
  • artifact is available, but its sources are not

The Nexus solution

One solution to such problems is to run an instance of (Nexus) repository manager, upload the missing artifacts there and configure it in you project’s POM. (Actually, a repository manager should be used even when all artifacts are available in public repositories, but that is a different story :)).

But what if this is not an option? What if – for political or technical reasons – you really need to carry some dependencies with your project? How do you do it?

The “system scope” attempt

It is possible to describe in-project dependencies using “system” scope and URL that references a jar co-located with the project. Unfortunately, system-scoped dependencies break the transitive dependency resolution of Maven. (Also, if your in-project dependency is needed only for tests, there is no way to specify a “test” scope for it…)

The in-project repository

To embed a Maven repository in the project:
Create a subdirectory “lib” in the project directory.
Define this subdirectory as a repository in the project’s POM:


<repository>
    <id>lib</id>
    <name>lib</name>
    <releases>
        <enabled>true</enabled>
        <checksumPolicy>ignore</checksumPolicy>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <url>file://${project.basedir}/lib</url>
</repository>

(Above assumes that you want to turn off the SHA1 checksum checking for your in-project repository.)

For an artifact with groupId x.y.z, content of the “lib” subdirectory should be:

lib
|– x

      |– y
          |– z
|– ${artifactId}
                  |– ${version}
                      |– ${artifactId}-${version}.pom
                      |– ${artifactId}-${version}.pom.sha1
                      |– ${artifactId}-${version}.jar
                      |– ${artifactId}-${version}.jar.sha1

POMs for in-project dependency artifacts are not, strictly speaking, necessary. But if you do not have them, Maven will attempt to retrieve the POMs from the other repositories (including central) for each build.

SHA1 checksums are not necessary either – if you indicate in the repository definition (see below) that they should be ignored.

This structure can be created manually – or using Maven itself, thus reducing the risk of typos (thank you +vmp for the idea and +Jeremy for the generatePom tip!):

Install the artifact in the local repository with
mvn install:install-file -Dfile=myArtifact.jar -DgroupId=x.y.z -DartifactId=${artifactId} -Dversion=${version} -Dpackaging=jar -DgeneratePom=true

and then copy resulting files from the local repository into the in-project one.

Obsolete “legacy” layout

It is somewhat simpler to use the Maven1 repository layout. That means that under lib directory there are directories for each groupId that you need:

lib
|– ${groupId}
|– poms
|– ${artifactId}-${version}.pom
|– jars
|– ${artifactId}-${version}.jar
|– java-sources
|– ${artifactId}-${version}-sources.jar

Sub-directory “java-sources” is needed only if you want to publish the sources.

In the repository definition, add:

...
<repository>
   <layout>legacy</layout>
</repository>

With Maven 3, “legacy” layout is going away, so the default layout has to be used.

Modules

For multi-module projects, a “lib” repository declared in the parent POM is inherited by each module. Artifacts are being looked up in the “lib” folder under each module. Where to put arftifacts depends on the build order: the first module that needs an artifact has to contain it in the “lib” folder; figuring this out is tedious – and empty “lib” folders left around by the build are not pretty 🙂

One approach to deal with that is to declare the “lib” repository in a separate module dedicated to the in-project repository. That module contains all the artifacts in its “lib” folder and declares them as dependencies, so that the local Maven repository is populated during the build of the module.

The modules that actually use an artifact carried in the in-project repository can declare a dependency on the “lib” module, ensuring that the needed artifact will be present in the local repository prior to the module’s build. Unfortunately, *all* of the in-project artifacts will transitively become the module’s dependencies.

Or: leave the “lib” module out of the dependencies of the rest of the modules, and ensure that the local repository is populated by running a build of that module manually (once on the empty system and every time the in-project dependencies change).

Mirrors

For this to work when using a mirror, in-project artifacts have to be excluded from mirroring. One way to do it is: in the mirror definition’s “mirrorOf” element, put “external:*” instead of (to inclusive) “*”. (Thanks for the tip, anonymous commenter!)
Advertisements

Maven: Command to update repository after adding dependency to POM

Posted on

  • mvn install (or mvn package) will always work. You can use mvn compile to download compile time dependencies or mvn test for compile time and test dependencies but I prefer something that always works.
  • If you want to only download dependencies without doing anything else, then it’s mvn dependency:resolve. Or mvn dependency:get -Dartifact=groupId:artifactId:version for a single dependency.

[mvn install] How To install Jar File Into Maven Local Repository?

Posted on Updated on

There are cases that you need to issue Maven’s command to include a jar into the Maven local repository manually:
  1. The jar you want to use doesn’t exist in the Maven center repository.
  2. You created a custom jar, and need to use for another Maven project.
Example
For example,  spring4gwt.jar , a popular third party Java library, which is used to bridge spring and gwt, but it’s not available in the Maven center repository.
In this tutorial, I will show you how to install the “spring4gwt.” jar into your Maven’s local repository.
mvn install:install-file -Dfile=C:\spring4gwt.jar -DgroupId=com.google.code -DartifactId=spring4gwt -Dversion=0.0.1 -Dpackaging=jar
Demo.
C:\Users\guetampan\bluerock\workspace\mweb-1.0>mvn install:install-file -Dfile=C
:\Users\guetampan\bluerock\workspace\mweb-1.0\spring4gwt.jar -DgroupId=com.googl
e.code -DartifactId=spring4gwt -Dversion=0.0.1 -Dpackaging=jar
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for
com.bluerock.mweb:mweb:war:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-inst
all-plugin is missing. @ line 223, column 12
[WARNING] 'build.plugins.plugin.version' for org.codehaus.mojo:build-helper-mave
n-plugin is missing. @ line 177, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten t
he stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support buildin
g such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building mweb 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install-file (default-cli) @ mweb ---
[INFO] Installing C:\Users\guetampan\bluerock\workspace\mweb-1.0\spring4gwt.jar
to C:\Users\guetampan\.m2\repository\com\google\code\spring4gwt.0.1\spring4gwt
-0.0.1.jar
[INFO] Installing C:\Users\GUETAM~1\AppData\Local\Temp\mvninstall137699429999557
0548.pom to C:\Users\guetampan\.m2\repository\com\google\code\spring4gwt.0.1\s
pring4gwt-0.0.1.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.089s
[INFO] Finished at: Tue Jan 08 16:06:54 SGT 2013
[INFO] Final Memory: 5M/121M
[INFO] ------------------------------------------------------------------------
C:\Users\guetampan\bluerock\workspace\mweb-1.0>

To check whether the file is in your maven folder, open your maven repository directory, in windows it should be under your {user directory}\.m2\repository. Following is the screenshot:
Now the spring4gwt.jar is ready to use. add the following into your pom.xml

<dependency>
<groupId>com.google.code</groupId>
<artifactId>spring4gwt</artifactId>
<version>0.0.1</version>
</dependency>

Maven – Generate web project for eclipse with maven

Posted on

To generate an empty project from the scratch you just need 2 command line:

1. generate a web project with maven
2. generate eclipse project.

1. Generate web project with maven:


Generally, to generate a project from maven, we need to do the following command:


mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Usage example:

mvn archetype:generate -DgroupId=net.webanalytics -DartifactId=webapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false.

2. Convert to  eclipse project:

mvn eclipse:eclipse -Dwtpversion=2.0

What version of Eclipse does WTP work with?

  • WTP 1.0.x runs on Eclipse 3.1.x.
  • WTP 1.5.x runs on Eclipse 3.2 (a.k.a. Callisto).
  • WTP 2.0.x runs on Eclipse 3.3 (a.k.a. Europa).
  • WTP 3.0.x runs on Eclipse 3.4 (a.k.a. Ganymede).
  • WTP 3.1 runs on Eclipse 3.5 (a.k.a. Galileo).
  • WTP 3.2 runs on Eclipse 3.6 (a.k.a. Helios).
  • WTP 3.3 runs on Eclipse 3.7/4.1. (a.k.a. Indigo)
  • WTP 3.4 is intended for Eclipse 3.8/4.2. (a.k.a. Juno)