当前位置: 首页 > 知识库问答 >
问题:

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

章宏恺
2023-03-14

我只是按照这个问题修改了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>

下面是修改后的(要创建cambelton.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>

共有1个答案

子车劲
2023-03-14

在Maven Assembly插件配置中,检查

<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,但您将其重命名为Cambelton,因此您还应该使其与此更改一致(${project.build.directory}/Cambelton.war,而不是${project.build.directory}/${project.artifactId}.war)。这很可能导致生成错误
  • 我将用${project.basedir}/src替换它,而不是指向/${project.artifactId}/src(对于WebContent输出目录也一样)

 类似资料:
  • 我正在使用maven Struts2空白工件来创建我的web应用程序。以下是我的pom.xml:- 上面的pom.xml使用jetty服务器创建war文件。因此,当我运行mvn jetty:war-run时,它会创建名为myArtifact-3.war(artifactId-version)的war文件,但我希望我的war文件名为myweb.war。我在pom.xml的配置中使用了war标记来指定

  • 我有一个多模块Maven projet,我想构建一个包含所有要在生产服务器上交付的文件的zip文件,这意味着一个war文件和一堆shell脚本。 这里是我父母pom的一些摘录。xml: war文件在eloi web项目中生成。所以我有一个问题,因为父母是pom。xml是首先执行的,所以我实际上得到了我的zip文件,但是在构建之前,使用了war文件的旧版本,如果我先清理,它就会崩溃。我不知道如何解决

  • 我目前正在通过maven插件使用swagger-codesen:https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen-maven-plugin.我已经设置了我希望使用

  • 问题内容: 手指交叉可以帮助我! 我正在使用SmartSprites将目标网页上的PNG合并为一个,以便更快地加载。 SmartSprite将检查您的CSS文件,生成CSS Sprite图像,并创建一个新的CSS文件,该文件将使用此Sprite图像而不是原始图像。我想要做的是在我的Maven WAR构建期间自动用SmartSprite替换原始CSS文件。 所以这就是我想发生的事情: SmartSp

  • 这段代码第一次可以运行得很好,但是当我第二次运行app时,它不会立即生成PDF文件。

  • 我正在尝试创建一个方法,对象被传递到该方法,并读取所有字段,以便为null且为String的字段被赋予“”的值。 现在问题出现在我的课堂上。我有这个模型: 我做了这个方法:private ObjectMapper obMapper=new ObjectMapper();