110

I have the following plain pom running by Maven 3.0.4.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>test</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

</project>

I am trying to override default settings in command line like this:

mvn -Dproject.build.finalName=build clean package

But this this is ignored, and I get test-1.0.jar. I've tried to change another properties, like outputDirectory, directory, artifactId, but also failed.

What is the proper way to do this thing?

lpacheco
  • 976
  • 1
  • 14
  • 27
glaz666
  • 8,707
  • 19
  • 56
  • 75

1 Answers1

157

See Introduction to the POM

finalName is created as:

<build>
    <finalName>${project.artifactId}-${project.version}</finalName>
</build>

One of the solutions is to add own property:

<properties>
    <finalName>${project.artifactId}-${project.version}</finalName>
</properties>
<build>
    <finalName>${finalName}</finalName>
 </build>

And now try:

mvn -DfinalName=build clean package

thecoshman
  • 8,394
  • 8
  • 55
  • 77
Andrzej Jozwik
  • 14,331
  • 3
  • 59
  • 68
  • 7
    Is that the only way to do that? What if I can't make changes to the POM file? – glaz666 Dec 14 '12 at 12:02
  • 1
    You can not override `artifactId` and `version`. So you can create next project add include in dependencies current one only. Then you can make what you want. – Andrzej Jozwik Dec 14 '12 at 13:43
  • 2
    I need to override `finalName` through command line without changing POM. Is that doable? – glaz666 Dec 14 '12 at 14:22
  • 3
    Ok, it is unavailable, because you only can override user-defined properties, not Maven Properties because they are properties of Model class. The solution is described in the answer. – glaz666 Dec 17 '12 at 11:41
  • 12
    For overriding multiple parameters, use multiple -D flags. If anyone is wondering. – Matthias Jul 22 '16 at 16:21
  • 5
    Additionally, if the property you are trying to override has periods, you might need to enclose it in single quotes like: `mvn '-Dproject.build.finalName=build' clean package` – Xantix Mar 11 '17 at 07:28
  • 1
    @Xantix Why would you need to to this? Do you have a shell for which dots are special characters? – scravy Feb 20 '18 at 14:40
  • 1
    @scravy It gives an error like: "[ERROR] Unknown lifecycle phase ".build.finalName=build". You must specify a valid lifecycle phase or a goal in...". FWIW, I use Powershell. – Xantix Feb 21 '18 at 17:58
  • The maven assembly plugin has a property called skipAssembly. The reference says that the user property for this is "assembly.skipAssembly". [ref] (http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html) I've set it to true in the pom. I've tried setting this false on the command line but it gets ignored. `mvn -Dassembly.skipAssembly=false clean install` Any thoughts? – dnuttle Jun 24 '20 at 15:16
  • How to move this war in artifactory ? – Shankar Jul 09 '20 at 23:49
  • Just `mvn deploy`? – Andrzej Jozwik Jul 10 '20 at 11:19