How do I copy a file on the same path as a dependency jar in Maven? - maven

I have XYZ.jar and XYZ.properties from a 3rd party.
XYZ.jar has some code which looks for XYZ.properties at the path as XYZ.jar.
My problem is packaging my project which uses XYZ.jar and XYZ.properties.
I've added the jar to our repo using mvn deploy:deploy-file.
mvn deploy:deploy-file -Dfile=xyz.jar -Durl=<someurl> -DgroupId=thirdparty.xyz
-DartifactId=xyz -Dversion=1.0 -Dpackaging=jar -DrepositoryId=thirdparty
I've also added a dependency for XYZ in my project pom.xml like:
<dependency>
<groupId>thirdparty.xyz</groupId>
<artifiactId>xyz</artifactId>
<version>1.0</version>
</dependency>
My question is how do I add the properties file? The two restrictions on it are
1.XYZ.properties has to be on the same path as the XYC.jar
2. The name of the properties file has to be XYZ.properties. No version number/artifactid should be appended to the name.
How do I do this in maven?
I've tried packaging the properties file and add a dependency, but the path is different - i.e. it goes into its own group-artifact-version folder.
Some Clarifications :
The rest of my project has standard maven structure
Unfortunately, I cannot change the way the third-party XYZ.jar works.

You don't. You can deploy the properties file the same way with a different packaging if you like and then use that packaging in the dependency to it but you will not be able to dictate the name in the repo.
You can however use the dependency plugin to retrieve the artifact and give it the name you desire again..
Overall however it would be better to have the properties file in the jar..

Related

Setting name of jar file while using mvn install:install-file

So previously I was trying to find a way to install jar file which is built in my project to the .m2 folder via run configuration support.
Link for reference.
My main concern then was to not keep any hard coded values in command and to pick most data from pom.xml file. This was achieved successfully, but now I have another problem.
In the project, I have 2 modules module1 and module2.
When module1 is built, it generates 2 files a war file since it is a web based application and second one is jar file which is used to satisfy dependencies of other modules.
The jar file is generated using
<attachClasses>true</attachClasses>
property set in the maven-war-plugin in pom.xml of module1.
So if the module1 artifact id is set as module1-corp, then the jar file is named as module1-corp-classes.jar if the jar is installed using maven-install-plugin. But due to the legacy structure of the project, maven-install-plugin cannot be used and I have to use maven command line via Intellij run configurations to install this file.
So the command I used is
mvn install:install-file -Dfile=${project.build.directory}/${project.build.finalName}.jar -DgroupId=${project.groupId} -DartifactId=${project.artifactId} -Dversion=${project.version} -Dpackaging=jar
This installs the jar file perfectly, only it doesn't append the classes part at the end of jar file. so my jar file is now installed as module1-corp.jar instead of module1-corp-classes.jar which is not working okay with modules which are dependent on it.
I suspect this is due to the way module1 dependency is accessed in module2 which is as follows:
<dependency>
<groupId>module1.groupid</groupId>
<artifactId>module1.artifactId</artifactId>
<version>${module1.version}</version>
**<classifier>classes</classifier>**
</dependency>
This code is in the module2 pom.xml. I believe the classifier part is what is causing the issue, but I cannot change this since it is a legacy project.
So in the end I have two options only
Rename the jar while it is being installed via maven command line
Some other way which can rename the jar via an Intellij run configuration.
I tried using following flag
mvn install:install-file -Djar.finalName=jarname
But this doesn't seem to work as expected.
The install maven plugin allows also to specify the classifier (see: here). So in your example the command would need to be changed to:
mvn install:install-file -Dfile=${project.build.directory}/${project.build.finalName}.jar -DgroupId=${project.groupId} -DartifactId=${project.artifactId} -Dversion=${project.version} -Dpackaging=jar -Dclassifier=classes

Maven goal to move file to another directory

I need to move the war generated to some other directory with maven goal.
I know how to move it to another location by adding configuration in pom.xml but i need to do it with a goal.
Which i can run directly without using pom.xml
like: mvn jboss-as:deploy-only deploys jboss with war generated without including any plugin specific configuration in POM.
Their are some restrictions to plugins which can be used in pom. So need to use Maven Goal only

Maven ant task to add system path jar to WAR file

Running into a small problem. I have a spring-maven project. And there are some external jars I need to add into the POM which I did using .
Now to build the WAR file we are using an Ant Maven task i.e. artifact:mvn providing the argument war:war.
Here somehow my external jars are not getting added to the WAR file i.e. WEB-INF/lib
Can some one please let me know if I am missing something. Below is my pom entry
<dependency>
<groupId>{test}</groupId>
<artifactId>Test</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/test.jar</systemPath>
</dependency>
Best solution is to start using a repository manager (a.k.a "Maven proxy server") and install the needed artifacts (test.jar) into the repository manager and use it as a usual dependency instead of using system scope via systemPath.
Calling mvn war:war via Ant does not make sense and shows you should learn how Maven works.
Change the packaging type in your pom file to war and you can simply call maven via:
mvn clean package
and everything should work. But this is only gues cause you didn't show your full pom file.
Install the test.jar locally using mvn install:install-file (docs). Now you can remove the system scope (and the systemPath) and everything will work out of the box.

Maven: Deploying a jar/war with built in pom.xml file

I don't have a Maven project. What I have is a jar with the pom.xml embedded in the file. When I deploy this via the Artifactory GUI, Artifactory finds the pom and deploys it with the jar. It even recognizes the groupId, artifactId, and version.
When I try using mvn deploy:deploy-file it fails. If I extract the pom, and run mvn deploy:deploy-file with the -DpomFile=pom.xml, it works. Is there a way of deploying this jar with the embedded pom via the Maven command line? Or, do I need to extract the pom.xml first?
I have not heard of the possibility to specify the pom file from archive. I think it is unlikely to be an option, because Maven itself is just a light-weight program, which runs with plugins; and it needs some configurations to run with; and all references to plugins to be used are in those files.
Consider writing an Ant script that will extract the file, run mvn deploy:deploy-file -DpomFile=pom.xml and then delete the file.
The solution looks not very nice, I know, but it should help.
This is an Artifactory feature and not standard Maven behaviour.
Keep in mind that, for example, if you use dependency:unpack-dependencies or the assembly plugin to create some sort of über jar there would be multiple pom.xml files within the jar under the /META-INF/ path so it would be very difficult to select which pom was the true pom.

Adding Jar files to Maven

I have started to work on maven project recently.
The piece of code I am writing needs JAR files that are not a part of the project.
When I add the JARS TO THE build path and use a mvn clean install, the build is failing with errors saying that the classes that were supposed to be in the jar were not found.
Is there anything that i am missing?
Is there a different way to add the JAR's in maven projects?
If the JARs are already hoisted in some public Maven repositories, add them to the <dependency> section in the pom.xml . You may have to configure the address of these public Maven repositories in the <repositories> section in the pom.xml in order to cause Maven can connect them.
Otherwise , you have to use the install command to include these JARs into the your local repository and then add their <dependency> section in the pom.xml
The command to install the JARs into your local repository:
mvn install:install-file -DgroupId=com.abc -DartifactId=XXXXX
-Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jars
It sounds like you don't understand how Maven works. You don't add jars to the build path, you declare them in the pom, and let Maven download to the local drive, and it adds it to the build path for you. I would recommend you read this 5 minute intro, and understand how the dependency management works.

Resources