Is it possible to change the finalname of external jar in the pom file in Maven?
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>iText</artifactId>
<version>2.1.7</version>
</dependency>
I would like to change it so only this specific jar name would be in the camel case. Instead of itext-2.1.7.jar I would like to have iText-2.1.7.jar. How Can I achieve it?
There's no direct way. So as an ugly workaround;
Add a sub module to your project and add this dependency there and package using assembly plugin. Change the sub module's artifact ID as you wish. (Basically this new module will be the wrapper for this dependency). In the runtime, the classes will be available.
You should define this module as the top element of the <modules> tag. So that it will be built first. and updated in the .m2. That also makes sure that your code will compile just fine any where without referring to the artifact from the maven central.
Again, there's no OOTB way. Hence this.
Related
I would like to add a dependency to my maven project so that Maven can resolve the dependency, but not add it to the classpath. E.g.
<dependency>
<groupId>com.example</groupId>
<artifactId>example-artifact</artifactId>
<type>bin</type>
<classifier>jar-with-dependencies</classifier>
<version>1.0</version>
</dependency>
In this particular case, the dependency is a FAT executable jar file, that I want to run from my Mojo. I don't want the contents of that jar file to pollute my mojo's classpath. I just want it treated as a binary file, so that I can look it up inside my mojo (via project.getArtifacts(), and then execute it as CLI.
I've tried both "zip" and "bin" packaging types, but it always seems to end up on the classpath.
I could bundle the zip inside another jar dependency and then extract/run it at runtime, but I would prefer to just figure out how to resolve it but exclude it from the classpath.
Any ideas?
How do i update the maven local repository? if, i do not have the download rights.
I have the latest jar files with me. how do i replace the jar files in m2 folder and update the pom file?
I have tried this :
You can copy them in your local repository. For example, if you should add this dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
You should create folder in your maven local repository:
//com//fasterxml//jackson//core//jackson-databind//2.9.4 and put your jar in this folder.
Now getting below error -
failed to read artifact descriptor for org selenium selenium selenium-java:jar:3.13.0
You can copy them in your local repository.
For example, if you should add this dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
You should create folder in your maven local repository:
//com//fasterxml//jackson//core//jackson-databind//2.9.4 and put your jar in this folder.
You probably do not have the whole list of jars with all relevant parent poms, transitive dependencies etc. It is very hard to create a complete list manually. If any dependency of a dependency of a dependency is missing, your project won't compile.
First of all, I would advise you to try to get the "download rights" because using Maven with a bunch of manually deployed jars is very brittle and will take a huge amount of time to get right.
If you cannot get them, you either need to add all jars manually that Maven is missing (like the selenium jar mentioned in your question), or, if you are sure that certain artifacts are not necessary, you can exclude these dependencies in your pom.
I can't compile the files directly. I use mvn package.
I can't run the files directly. I use storm (Apache).
I don't know much about Maven.
I tried to just put the .jar in the same folder as the code and use import com.path.of.jar. It did compile, but when I tried to run, gave a NoClassDefFoundError.
Try this way to add dependencies directly, like this:
<dependency>
<groupId>sample</groupId>
<artifactId>com.sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>
When you work on a maven based project, you manage dependencies through the pom.xml file at the root of the project. POM stands for Project Object Model and contains information about the project and configuration details used by Maven to build the project (Introduction to the POM).
A maven project produces an artifact uniquely identified by its coordinates: The <groupId>, <artifactId> and <version> that you normally find at the top of your pom.xml file. Once an artifact is published to a repository other maven projects can depend on it.
If you look at the content of your POM file you should see a <dependencies> element containing all dependencies that your project needs. If you want to import classes from a jar in your code you will need to find the maven coordinates of this jar (for example on search.maven.org or mvnrepository.com).
Once you have the coordinates add a corresponding dependency section. It should look like this:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
Next time you run mvn package, the jar will be downloaded, used during compilation and packaged with your artifact.
And if you would like to get a good understanding of maven the following free book is excellent: Maven: The Complete Reference
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).
How do I take a jar file that I have and add it to the dependency system in maven 2? I will be the maintainer of this dependency and my code needs this jar in the class path so that it will compile.
You'll have to do this in two steps:
1. Give your JAR a groupId, artifactId and version and add it to your repository.
If you don't have an internal repository, and you're just trying to add your JAR to your local repository, you can install it as follows, using any arbitrary groupId/artifactIds:
mvn install:install-file -DgroupId=com.stackoverflow... -DartifactId=yourartifactid... -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jarfile
You can also deploy it to your internal repository if you have one, and want to make this available to other developers in your organization. I just use my repository's web based interface to add artifacts, but you should be able to accomplish the same thing using mvn deploy:deploy-file ....
2. Update dependent projects to reference this JAR.
Then update the dependency in the pom.xml of the projects that use the JAR by adding the following to the element:
<dependencies>
...
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
You can also specify a dependency not in a maven repository. Could be usefull when no central maven repository for your team exist or if you have a CI server
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>commons-utils</artifactId>
<version>1.3</version>
<scope>system</scope>
<systemPath>${basedir}/lib/commons-utils.jar</systemPath>
</dependency>
Actually, on investigating this, I think all these answers are incorrect. Your question is misleading because of our level of understanding of maven. And I say our because I'm just getting introduced to maven.
In Eclipse, when you want to add a jar file to your project, normally you download the jar manually and then drop it into the lib directory. With maven, you don't do it this way. Here's what you do:
Go to mvnrepository
Search for the library you want to add
Copy the dependency statement into your pom.xml
rebuild via mvn
Now, maven will connect and download the jar along with the list of dependencies, and automatically resolve any additional dependencies that jar may have had. So if the jar also needed commons-logging, that will be downloaded as well.
I'd do this:
add the dependency as you like in your pom:
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
run mvn install it will try to download the jar and fail. On the process, it
will give you the complete command of installing the jar with the error message. Copy that command and run it! easy huh?!
I'll assume that you're asking how to push a dependency out to a "well-known repository," and not simply asking how to update your POM.
If yes, then this is what you want to read.
And for anyone looking to set up an internal repository server, look here (half of the problem with using Maven 2 is finding the docs)