Changing the name of the generated war file from command line with maven - maven

According to the maven war plugin documentation I should be able to set the name of the generated war file with the parameter warName. Is it not possible to do this from the command line with mvn -DwarName=mySpecificName package? When I run maven like this the war file still gets the default name.
My webapp project is part of a multi module project and I only want to change the final name of the war file, not any other projects generated artifact.
I am using maven 3.0.4 and version 2.3 of the war plugin.

You can achieve the same effect by maven property.
1) Define a property via
<properties>
<my.warName>xxx</my.warName>
</properties>
You can overwrite the default value by "-Dmy.warName=commandlineWarName"
2) Redefine the war name
<build>
<finalName>${my.warName}</finalName>
<!-- ... -->
</build>

After looking at the code of the war plugin I realize that it is not possible to set the warName parameter from command line. I assumed that all parameters were possible to set from the command line. This assumption was incorrect.

Related

How to use maven run parameters in the maven project?

I am having a new Maven project and I want to pass parameters through command line..
This is my requirement -
If I pass - install -Dinfra=local then my test should run on local machine
If I pass - install -Dinfra=ip then my test should run on the machine having desired ip.
I just wanted to know how to configure this infra into my project so that I can access that through command line.. Many thanks!
You can define a property in your POM:
<project>
...
<properties>
<infra>local</infra>
</properties>
...
</project>
Then you can reference it in the POM by using ${infra}. You can overwrite the value of the property through the command line (as in your example).
Also see https://stackoverflow.com/a/13709976/927493

Maven: Define SuperPOM property project.build.directory over command line

I am building my maven project with GitLab CI on a docker file.
I would like to configure my pipeline with a "compile" stage and a "test" stage. To be able to do that, I need to set the property project.build.directory, which is defined in the maven super POM, to the docker cache so the compiled artefact does not get lost between the jobs.
project.build.directory is a predefined maven property. Therefore I would think that I am able define it with the CL parameter -Dproject.build.directory=anotherDir. This somehow does not work and my project still gets built to the default directory target.
If I modify my POM with
<properties>
<buildDir>target</buildDir>
</properties>
<build>
<directory>${buildDir}</directory>
</build>
and call mvn clean install -DbuildDir=customTargetDir, my project gets built to the customTargetDir as expected.
Why is that? I really don't see a difference. I both cases, I define the value of an existing property.

Execute Maven plugin with custom classpath entry

I would like to call a maven plugin with a custom entry on its classpath. This is usually possible by adding <dependencies> inside the <plugin> tag. The problem is, what I would like to add to the classpath is not a maven artifact, but some random folder in my project (the reasons for this are quite obscure, I need a resource file to be present of the classpath, but I must not copy it to the /target/classes due to IDE shenanigans).
Is there any way to specify truly arbitrary classpath entries for a maven plugin?

Version in jar name

When I import mvn project into Intellij the jar file it generates doesn't include version. But mvn generated jar has name-version.jar format. So I end up with two jar files one with version and another without one. I can of course, change module name in Intellij settings to include version. But that will be reset whenever I change pom file.
Maybe somebody else had a better idea?
The jar name that Maven generates on disk is controlled by /project/build/finalName so if you edit your pom.xml to look like
<project>
...
<build>
...
<finalName>${artifactId}</finalName>
...
</build>
...
</project>
and then Maven will be generating the jar file without the version.
Note
finalName only controls the name of the file on disk. Once that file is transferred into the local repository cache or a remote repository it will be renamed to match the repository layout (i.e. ${artifactId}-${version}.${type} or ${artifactId}-${version}-${classifier}.${type} for artifacts with a classifier). You cannot change the format used by the repository.
I add the above note because the first thing everyone seems to want to do upon learning about the finalName parameter is try and change the name in the repository.
use version tag of the maven
<version>0.0.1-SNAPSHOT</version>
do not let intellij to create jar files without version tag.

Is there a way to add a custom line in .classpath using mvn eclipse:eclipse?

I am using mvn eclipse:eclipse command to generate my .project and .classpath.
However, for some reasons, I want to add one line in the .classpath file. Is there a configuration in my pom.xml that I can use to achieve that?
Note that <additionalConfig> cannot be used, as this will erase the content of the .classpath.
I am using maven 3.0.2 and maven-eclipse-plugin 2.8.
It depends on what that line is.
If it's a source folder, use the
buildhelper-maven-plugin to
add a source folder in a
lifecycle phase before
generate-sources, this will
automatically be picked up by the
eclipse plugin.
If it's a classpath container, you
can use the classpathContainers
parameter
If you want to change the output folders (from target/classes and target/test-classes to something else), change them in the maven build configuration:
<build>
<!-- replace "target" -->
<directory>somedir</directory>
<!-- replace "target/classes" -->
<outputDirectory>anotherdir</outputDirectory>
<!-- replace "target/test-classes" -->
<testOutputDirectory>yetanotherdir</testOutputDirectory>
</build>
You can configure each of those three independently, and the changes will be picked up by the eclipse plugin, but it's considered good practice to put outputDirectory and testOutputDirectory inside directory (usually by referencing ${project.build.directory}), otherwise you break standard functionality like mvn clean (it cleans ${project.build.directory}):
<build>
<directory>bin</directory>
<outputDirectory>${project.build.directory}/main-classes
</outputDirectory>
<!-- this config will replace "target" with "bin",
compile src/main/java to "bin/main-classes"
and compile src/test/java to "bin/test-classes"
(because the default config for <testOutputDirectory> is
${project.build.directory}/test-classes )
-->
</build>
Reference:
POM Reference > Build > The BaseBuild Element Set
POM Reference > Build > Directories
Introduction to the POM > Available Variables
Update: in your case I guess the only possible solution is to programmatically edit the .classpath file. What I would probably do is something like this:
Define a <profile> named eclipse (or whatever)
Define an execution of the gmaven plugin (use this version)
Write a short groovy script (inline in the pom or external) that checks the .classpath file for your classpath container and adds it if missing (bind execution to a lifecycle phase, e.g. generate-resources)
set the profile activation to <file><exists>${project.basedir}/.classpath</exists></file> (because you only want it to be active in an eclipse project)
The problem with this solution: eclipse:eclipse is a goal, not a phase, so it's not possible to execute this automatically, so you'll have to do something like this:
mvn eclipse:eclipse # two separate executions
mvn generate-resources # here, profile will be active
or perhaps this will also work:
mvn -Peclipse eclipse:eclipse generate-resources

Resources