Deploy mutliple artifacts to the same folder using maven - maven

Currently we have one main pom file which builds the code for multiple modules using the module tag. When I use the "mvn clean package deploy" command (which references the main pom file and performs these actions fro all other modules), the packaged file(war/jar) for each module is placed in it's respective target directory. Since there are different modules and they have their own respective group-id,artifact-id etc. the packaged files are spread across different folders.
My applications consists of all of these modules and I need all the packaged files under one single folder. Till now we have used an ant script to copy the relevant files from all of these modules to a single folder.
Apart from copying the files/aggregating all the packaged files and then uploading it as part of deploy:deploy-file is there any way I can deploy all the files to the same folder?

No, this is not possible.
The directory structure in Artifactory is always of the form
org/something/artifact/1.2.3/artifact-1.2.3.jar
assuming groupId org.something, artifactId artifact and version 1.2.3.
This structure cannot be changed.

Related

How to copy all the dependencies in Maven project modules to a directory?

I have a big local maven project that contains multiple modules which are inturn maven projects and are dependent on one another.
Ex.
parent pom.xml
<pom>
<module1> #jar
<module2> #dependent_on_module1.jar
<module3> #
</pom>
I have mentioned the sequence to build those modules in the parent pom.xml .
I also mentioned where to place the artifacts when they're built in groudId and artifactId.
But in the dependencies for all those modules, I have mentioned a common local system path for all those modules.
Is there any way to copy all the artifacts which are being created for modules when maven build is performed on the parent pom to a specific directory that can be dynamically mentioned when the maven command is run.
I have searched for maven copy command. But looks like it's not going to do what I want.
Any suggestions?

target folder vs local repository

I know that Maven houses the outcome of the build in the local repository (artifacts goes installed under ~/.m2/repository/), but it also outputs the compiled classes in the target folder next to src.
Is there any difference between what goes in the local repository and what goes in the target folder?
They are completely different and shouldn't be mixed up.
target represents the build directory. This is to say, every temporary file that is generated during the build from the sources ends up there. Quite notably, you'll find the compiled classes of the main and test Java sources, but you'll also find lots of things in there (generated source files, filtered files, etc.). What matters, is that everything that is contained in this folder is inherently temporary. You can delete it at any time, running mvn clean, and be assured that the next build will (or at least should) work just fine. All the files and folders generated under target serve a single purpose: create the artifacts of the project. A Maven project, for example with jar packaging, will have a single main artifact, which is composed of its final name with a jar extension, and will contain the compiled Java classes. The final name can be a custom name, set within the POM, or the default one derived from the Maven coordinates of the project. Such a project can also have additional attached artifacts, like a test JAR, or a sources JAR.
The local repository only contains the artifacts. There are no temporary files in there. What is installed when running mvn install is strictly the generated artifacts of the Maven project, i.e. the end products, plus the POM file of the project. Everything that served to create them isn't put in the local repository, and the build of a project must never put temporary things in there. Keep in mind that the local repository is a Maven repository, and, as such, follows a strict naming scheme: a project with a group id of my.groupid, an artifact id of my-artifactid and a version of 1.0 will get installed in the folder my/groupid/my-artifactid/1.0; in which you'll find the POM file, and all the other artifacts. The name of the artifacts themselves cannot be overriden: it will be my-artifactid-1.0.jar for a JAR project (perhaps with a classifier added).
This is generally a source of confusion: the name of the main artifact file that is generated under the target folder is completely distinct from the name that it will have in the local repository when installed, or in remote repositories when deployed. The first can be controlled, but the latter is defined by the naming scheme of the repository, which is calculated from the coordinates.
To recap: target contains all the gory temporary details during the build which creates the artifacts of a project (main JAR, sources, Javadoc... i.e. everything that is supposed to be deployed and released by that project), while the local repository (and remote repositories) will contain only the artifacts themselves.
Not much in terms of the generated module.jar if that's what you are really concern about. The .jar generated is the same, also considering recompiling the code would clean your /target folder but not the .m2 one.
Though /target folder would generally be composed of the compiled source classes /target/classes and /target/generated-sourceetc along with a module.jar.
On the other hand the local ~.m2/repository would consist of module.jar along with the pom.xml for that module and all the configs(repositories, dependencies etc) to rebuild that module from if required.

How do I build an exploded WAR in a multi-module Maven project and also include sibling JAR files in its WEB-INF/lib directory?

I’m using Maven 3.3.3 with the Maven WAR 2.6 plugin. I have a multi module project with both JAR and WAR projects within it. My WAR projects depend on my JAR projects, however, I do not want to package my WAR files, opting instead to keep them in an exploded form. So I tried running this command
/usr/local/apache-maven-3.3.3/bin/mvn -B -f /home/jboss/.jenkins/jobs/subco/workspace/pom.xml -B clean prepare-package war:exploded -DskipTests -T 3
Although this keeps my WAR exploded, the JAR projects do not get included in my web application’s target/myproject/WEB-INF/lib directory. Instead what is included are empty directories where my JAR files would have been, for instance
.jenkins/jobs/subco/workspace/myproject/target/myproject/WEB-INF/lib/child-jar-module-2.0.0-SNAPSHOT.jar/
How can I keep an exploded WAR, include my JAR files, and do it all from the command line (as opposed to creating a custom profile in my parent pom to do it for me)?
Edit: If you think this is a duplicate of another one, notice the part of my question where i point out that empty directories are generated when using the "prepare-package" phase. I don't want that.

Download artifacts from local Maven Repository

Hey im new to maven and i was wondering if anyone knew if i had a bunch of files in a directory, can i make that directory a local repo for maven. the projects in there are not maven projects but would i be able to somehow downlaod those files onto another location using maven?
this is how the folder is organized:
patch
--new folder
----versions (multiple folders which contain zip files)
is there a way to getting the version folders and copying that to a different directory with a maven command like
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=org.hibernate:hibernate-entitymanager:3.4.0.GA:jar:sources -DoutputDirectory=/home/-Dmdep.useBaseVersion=true
You were very close to the correct solution - same plugin, different mojo:
$ mvn maven-dependency-plugin:copy-dependencies -DoutputDirectory=/path/to/output
This will dump all dependencies into the location of your choosing.
The -DstripVersion=true argument is useful if you want to have the jars by their canonical name without the version suffix (ie log4j.jar vs. log4j-1.2.17.jar).
Hope that helps.

How to include dependency files in maven assembly

I have a maven project which is configured (via the use of pom.xml and assembly.xml) to package a zip file containing both the compiled webapp (war file) and all files under src/main/folder alongside it whenever we run mvn clean package assembly:single.
This project imports/uses another maven project (which becomes a jar file) and that project in turn also imports/uses a third maven project (again a jar file).
The third project also contains some files inside src/main/folder as well which I'd like to be placed alongside the other files in the zip file whenever I build the main project (the one on top of the chain that becomes a war file).
Is it possible somehow to tell maven to include all files in a specific folder inside all it's dependencies (Or in one specific dependency) alongside in the zip file?
Start here: Maven: The Complete Reference - 8.5. Controlling the Contents of an Assembly
Note that if you deploy a Maven project with assembly:assembly (which you really should configure as a <build> plugin, not use on the command line), the assemblies get "attached" to the project and installed in the repository. You can then reach them using the Maven dependency notation, i.e. mygroup:myartifact:extrafiles:zip
I've found the Assembly descriptor <dependencySets> cumbersome to configure. They really are for the simple case (including the JAR files your application needs at runtime).
Here is the general approach I would take for your desired outcome:
Use Maven Dependency Plugin dependency:copy to download the specific files from the other projects, placing them under a sub-directory of target/
Configure your assembly descriptor to add those downloaded/extracted files to your final assembly artifact.
Alternatively, hack something together using Maven Ant Run Plugin.
You will need to produce an additional assembly from the third project to package up the extra files. Append the assembly id so it produces a package named something like third-1.0.0-extrafiles.zip
Then add this as a dependency of your first project using <type>extrafiles</type> in the dependency descriptor. In the assembly for the first project you'll have to tell it to "unpack" the dependencies of this type (so you don't get a zip in a zip)

Resources