I am trying to launch a maven project, that I always launched manually in Tomcat, in Jetty. For this I am using the maven-jetty-plugin.
When executing jetty:run It seems like everything goes fine, except for the fact that Jetty does not find the dependencies defined in the POM.
If I run jetty:war, the war is built properly, using the dependencies, so that works. Why is jetty:run not including my libraries in WEB-INF/lib (or anywhere else)?
If you need a dependency for Jetty which resides somewhere other than the main Maven repo, add the following:
<pluginRepositories>
<pluginRepository>
<id>repo-id</id>
<url>http://my_other_repo.edu</url>
</pluginRepository>
</pluginRepositories>
I don't believe Jetty will use the existing repositories in your POM.
It really depends on the dependencies you have defined. For example, if you have defined a JNDI resource in your jetty.xml file, you will need the jetty-plus dependency. However, you will need this dependency for the execution of the plugin only. Plugins can have <dependencies/> and that is where you need to define it.
If you have a list of Jetty-specific classes that the plugin can't find, the best way to look up the dependencies is to use a site like www.jarvana.com.
By 'dependencies', do you mean other projects?
If so, you will need to install (mvn:install) these into the local repository before jetty:run will see them.
It's possible that jetty:war is packaging them and placing them into the lib folder without installing into the local repo.
For me I had one .jar file in my repo that was not getting picked up by Eclipse. I didn't figure out how to fix it in Eclipse, so my work around was to ...
Add a duplicate of the .jar file to the repo under a different groupId (different path under ~/.m2) using this command:
mvn install:install-file -Dfile=dhcp4java-1.0.0.jar -DgroupId=a_abc.hack -DartifactId=dhcp4java -Dversion=1.0.0 -Dpackaging=jar
Reference the new groupId , which requires only to update this entry for the dependency in pom.xml :
<groupId>a_abc.hack </groupId>
Related
I have a dependency in pom on some library.
I want to make some changes in it, test it locally and if it will work fine - deploy it to remote repo.
So I have locally made some changes in this library, installed it as a jar, and want to replace in my main project remote library with the local one.
What is proper way to do it?
You can override default maven repository in project's pom:
<repositories>
<repository>
<id>central</id>
<url>file://d:/repo</url>
</repository>
</repositories>
When resolving dependencies, Maven looks in your local repository ($HOME/.m2/repository). So if you have installed your modified dependency into your local repository (e.g. through mvn install) then when you build your main project, it will be used.
To make this more obvious, you may want to change the versions being used in both the library and your main project POM, so that you can be sure your version is being used for testing.
You may also find this question/answer useful: How do I force Maven to use my local repository rather than going out to remote repos to retrieve artifacts?
Maven first looks in your local repository in
C:\Users\User\.m2\repository
and if it can't find the library, then it looks in remote repos. If I understand your question correctly, this should be happening automatically as long as you point the correct version in the POM.
I have a project with the following layout:
My goal was to have mvn looking in the project lib dir as an additional location for potential libs that would not be found in maven repository like j-text-utils.jar for example. So I added this in the pom.xml
<repositories>
<repository>
<id>lib</id>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
I took the idea from here: http://randomizedsort.blogspot.co.il/2011/10/configuring-maven-to-use-local-library.html
When running mvn compile, it fails to find the relevant libs in the project folder.
Is there anything wrong with the above?
Thx
There is nothing wrong to setup a file based repository. But first and foremost, your directory structure needs to conform to the groupid/artifactid. You should use
set localrepopath=C:\path_to_repo_rootdir
call mvn install:install-file -Dfile=xyz-1.2.jar -DgroupId=com.foo -DartifactId=xyz -Dversion=1.2 -Dpackaging=jar -DlocalRepositoryPath=%localrepopath% -DcreateChecksum=true
It will create directory com\foo\1.2 with all the pom.xml, jar files, checksum files under it.
Then you need to define the dependency for these newly installed artifacts in your own project pom.xml.
If you do not want to setup local repository and only want to add them to the compile classpath, you can consider using "system" scope dependency, but it will make your build not portable and is discouraged in general.
You can do that (just configure the maven-dependency-plugin properly), but I wouldn't suggest that.
There might be a few drawbacks with that direction (e.g., having the Jars there could get into the repository you're using, for many projects it's better to have only one Jar in a dedicated place of your HDD rather than having one of them in each and every project, etc.).
There is a jar file lets say "abc.jar" which maven dependency does not exist(ie created a jar by using java command of own classes). I want to add this jar as maven dependency so that at build time it will automatically copy that jar in lib folder as like other maven dependency. how i will do. please help .
Add it as a dependency with a system scope. See the docs here.
However, rather than adding it as a system dependency it might be better to mavenize the jar itself, then you can build and install it into your dependency management system.
Also, see this question: Can I add jars to maven 2 build classpath without installing them?
You can use the systemPath attribute in the dependency tag in the POM file of your project.
In your pom.xml, use the following snippet corresponding to abc.jar:
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>abc</groupId>
<artifactId>x</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>{path_to_abc.jar}</systemPath>
</dependency>
</dependencies>
The scope parameter corresponding to this artifact must be set to system, for the artifact to be picked up from the specified systemPath.
Hope this helps!
A normal maven dependency is always resolved by looking into a repository. So you must put your JAR file into a repository.
You could install your JAR into your local repository. Have a look at the install plugin. The install-file goal is your friend.
If other developers also need this JAR (because they are working with the same project), they either need to install it locally too, or - better - you deploy the JAR to a remote repository. Have a look at the deploy plugin. Here the deploy-file goal is your friend. For deploying artifacts, you need a repository manager like Nexus or Artifactory.
However, a dependency could also have the system scope (look at the other answers).
I have a dependency
<dependency>
<groupId>de.matthiasmann</groupId>
<artifactId>twl</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/TWL.jar</systemPath>
</dependency>
Then I execute mvn assembly:assembly. All natives files and remote maven libs are added, but there is no this jar.
UPDATE
When I am trying to run app by java -jar myjar.jar. It returns an error that there is no class from the above dependency (NoClassDefFoundError : de.matthiasmann.twl.ForExample).
I want to add classes from this jar to myjar.jar (the same what maven does with remote dependencies). How I can configure maven to do that?
See Maven 2 assembly with dependencies: jar under scope "system" not included for why system dependencies are not included and how you can work around it, specifically the mvn install:install-file code is what you want.
You cannot use systemPath, unless your Java EE server/container has that jar configured.
Remember that maven is development and compile time only. Once the war file is built, maven has no effect except for having placed all the desired jars into the WEB-INF/lib folder.
When you specify system scope, it means that it is your responsibility to ensure that the jar is present when the war is deployed. You already have a framework to do that and you do not wish to encumber your build dependency with that jar, but you have to make it available thro Maven only during development.
The other similar scope is "provided". e.g., JBoss or your corporate common deployment Tomcat framework already provides many of the jars like Spring and Hibernate that are loaded by the server startup and common to all apps in the server. Therefore, you would not want maven build to include those into the war file.
The right way, Maven gurus would tell you. is to have your own maven server and build whatever artefacts you need into that server. However, occasionally that is not possible.
Therefore, on such occasions, I create project level repository that is distributed with the project and checked into version control. I run the command mvn install to create a project level directory called, say, "project-repo".
http://maven.apache.org/plugins/maven-install-plugin/examples/specific-local-repo.html (Due to familiarity, most of the time, I build the repo by hand rather than run mvn install).
Then in the POM, I specify file://${project.basedir}/project-repo as one of the repositories. The caveat with this is that in Windows, the slashes other than the pair after "file://" has to be back-slashes when referring to Windows file system paths.
<repositories>
<repository>
<id>my-repo1</id>
<name>my custom repo</name>
<url>http://ho.ho.ho</url>
</repository>
<repository>
<id>project-repo</id>
<name>my project repo</name>
<url>file://${project.basedir}\project-repo</url>
</repository>
</repositories>
YOu can implement this in many ways refer the blog below
http://blog.valdaris.com/post/custom-jar/
If you have such an dependency the best solution is first to use a repository manager and simply put that dependency into the repository manager and afterwards use it as simple dependency.
I have a problem, I have a project which is based in a system that includes modules. This modules are other maven projects and are referenced from system POM. My problem is I'm sharing the system project with a workmate and we've got different modules.
So, is there a way to tell Maven that I want to include a module referenced in my POM only if this module exists? I mean, without compilation failure.
Thanks!
I would suggest to use profiles and activate them on file/exists option.
Use dependencyManagementis to pull all the dependency information into a common POM file, simplifying the references in the child POM file.
There are several solutions, depending on what you can do/want to achieve.
One approach is to install a Maven repository server at your company (in your local LAN or in a LAN that you and your colleague share). Build the system and then deploy the modules to the server.
In your build, you can add this new server like this (documentation):
<project>
...
<repositories>
<repository>
<id>my-internal-site</id>
<url>http://myserver/repo</url>
</repository>
</repositories>
...
</project>
Or you can both copy the base system and build it locally with mvn install. That will copy all artifacts into your local cache.
From there, you can reference them as usual. You don't need to include all the modules; just the ones you really need. That way, you and your friend don't have to use the same dependencies.