更改 Maven 生成的 war 文件名时出现问题

Issue in Changing the war file name generated by maven

我只是按照这个 question 修改了我的 pom 以创建不同的 war 文件。这里是原来的配置

<build>

    <finalName>${project.artifactId}</finalName>
    <plugins>
        <!-- Run with Jetty -->
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.10</version>
            <configuration>
                <scanIntervalSeconds>5</scanIntervalSeconds>
                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>
            </configuration>
            <executions>
                <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <scanIntervalSeconds>0</scanIntervalSeconds>
                        <daemon>true</daemon>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- Compile java -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <!-- Build war -->
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <groupId>org.apache.maven.plugins</groupId>
            <version>2.1.1</version>
        </plugin>
        <!-- Pack zips -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>webapp</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>LabSystem${packname}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>src/main/assembly/webapp.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

这里是修改后的(想创建Cambellton.war)

<build>

    <finalName>Cambellton</finalName>
    <plugins>
        <!-- Run with Jetty -->
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.10</version>
            <configuration>
                <scanIntervalSeconds>5</scanIntervalSeconds>
                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>
            </configuration>
            <executions>
                <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <scanIntervalSeconds>0</scanIntervalSeconds>
                        <daemon>true</daemon>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- Compile java -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <!-- Build war -->
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <groupId>org.apache.maven.plugins</groupId>
            <version>2.1.1</version>
        </plugin>
        <!-- Pack zips -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>webapp</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>Cambellton{packname}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>src/main/assembly/webapp.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

尝试构建时出现错误,但创建了 war 文件。我认为在尝试创建 zip 文件时会出现问题。这是错误信息

> [INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ LabSystem ---
[INFO] Packaging webapp
[INFO] Assembling webapp [LabSystem] in [F:\MyDocuments\Java\ZK7LabSystem\LabSystem\LabSystem\target\Cambellton]
[INFO] Processing war project
[INFO] Copying webapp resources [F:\MyDocuments\Java\ZK7LabSystem\LabSystem\LabSystem\src\main\webapp]
[INFO] Webapp assembled in [4554 msecs]
[INFO] Building war: F:\MyDocuments\Java\ZK7LabSystem\LabSystem\LabSystem\target\Cambellton.war
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored 
(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
[INFO] 
[INFO] --- maven-assembly-plugin:2.2:single (webapp) @ LabSystem ---
[INFO] Reading assembly descriptor: src/main/assembly/webapp.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.543s
[INFO] Finished at: Thu Dec 17 11:29:29 IST 2015
[INFO] Final Memory: 16M/348M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2:single (webapp) on project LabSystem: Failed to create assembly: Error adding file to archive: F:\MyDocuments\Java\ZK7LabSystem\LabSystem\LabSystem\target\LabSystem.war isn't a file. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

编辑 1

这是webapp.xml

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>webapp</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/main/java</directory>
            <outputDirectory>/${project.artifactId}/src</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/src/main/webapp</directory>
            <outputDirectory>/${project.artifactId}/WebContent</outputDirectory>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>${project.build.directory}/${project.artifactId}.war</source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
</assembly>

编辑 2: 也添加了 $ 符号,但仍然给出相同的错误 作为

<build>

        <finalName>$Cambellton</finalName>
        <plugins>
            <!-- Run with Jetty -->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
                <configuration>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                    <stopKey>foo</stopKey>
                    <stopPort>9999</stopPort>
                </configuration>
                <executions>
                    <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <scanIntervalSeconds>0</scanIntervalSeconds>
                            <daemon>true</daemon>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-jetty</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Compile java -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <!-- Build war -->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <groupId>org.apache.maven.plugins</groupId>
                <version>2.3</version>
            </plugin>
            <!-- Pack zips -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>webapp</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>Cambellton${packname}</finalName>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>src/main/assembly/webapp.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

在 Maven Assembly Plugin 配置中,勾选

<finalName>Cambellton{packname}</finalName>

{packname} 未被识别为 属性,因为缺少 $ 前缀。假设您在 pom.

中将 packname 定义为 属性

在您的原始配置中,它是

<finalName>LabSystem${packname}</finalName>

还要检查与您的 src/main/assembly/webapp.xml 文件的一致性,该文件可以包括任何或指定的生成的 war 文件,如下例所示:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  <id>zip</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}</directory>
      <includes>
        <include>*.war</include>
      </includes>
      <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
  </fileSets>
</assembly>

更新
根据您最近的编辑(添加程序集配置),我看到了一些可以改进的地方以及潜在的错误原因:

  • 你在源元素中定义的文件使用了项目的artifactId,但你将其重命名为Cambellton,所以你也应该使它与此更改保持一致(${project.build.directory}/Cambellton.war而不是${project.build.directory}/${project.artifactId}.war)。这很可能导致构建错误
  • 我不会指向 /${project.artifactId}/src,而是将其替换为 ${project.basedir}/src(与 WebContent 输出目录相同)