วิธีการแทนที่คุณสมบัติ maven ในบรรทัดคำสั่ง


93

ฉันมี pom ธรรมดาต่อไปนี้ที่รันโดย 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?

คำตอบ:


136

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


6
Is that the only way to do that? What if I can't make changes to the POM file?
glaz666

2
I need to override finalName through command line without changing POM. Is that doable?
glaz666

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

8
For overriding multiple parameters, use multiple -D flags. If anyone is wondering.
Matthias

3
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
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.