【问题标题】:Custom maven archetype doesn't generate sources defined in archtype.xml自定义 maven 原型不会生成 archtype.xml 中定义的源
【发布时间】:2019-06-05 14:45:31
【问题描述】:

我正在尝试创建一个自定义原型,它定义了大量 pom.xml 并包含几个源文件。

在使用此原型生成项目时,我在 archetype.xml 中定义的任何源都不包括在内(pom.xml 按预期生成)。

我的 archetype.xml 位于 src/main/resources/META-INF/maven 并定义了两个来源。

<archetype xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0 http://maven.apache.org/xsd/archetype-1.0.0.xsd">
<id>custom-archetype</id>

<sources>       
    <source>src/main/java/App.java</source>
</sources>
<resources>
    <resource>src/test/java/BatFile.bat</resource>
</resources>
</archetype>

App.java 位于 src/main/resources/archetype-resources/src/main/java。

BatFile.bat 位于 src/main/resources/archetype-resources/src/main/resources。

我还有一个位于 src/main/resources/META-INF/maven 的 archetype-metadata.xml,我用它来定义依赖版本

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="custom">
    <requiredProperties>
        <requiredProperty key="thorntail-version">
            <defaultValue>2.2.1.Final</defaultValue>
        </requiredProperty>
    </requiredProperties>
</archetype-descriptor>

我的根pom.xml如下

<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>uk.co.xxxxx</groupId>
<artifactId>custom-archetype</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Custom Archetype</name>
<description>Archetype for Custom projects</description>
</project>

我正在关注 maven Documentation 创建自定义原型,据我所知,我所做的一切都是正确的。

这是运行 mvn install 的结果

[INFO] Scanning for projects...                                                   
[INFO]                                                                            
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreade
d.SingleThreadedBuilder with a thread count of 1                                  
[INFO]                                                                            
[INFO] ------------------------------------------------------------------------   
[INFO] Building Custom Archetype 0.0.1-SNAPSHOT                                  
[INFO] ------------------------------------------------------------------------   
[INFO]                                                                            
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ custom-archetype ---                                                                         
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!                                                   
[INFO] Copying 6 resources                                                        
[INFO]                                                                            
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ custom-archetype ---                                                                            
[INFO] Nothing to compile - all classes are up to date                            
[INFO]                                                                            
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ custom-archetype ---                                                                 
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!                                                   
[INFO] Copying 0 resource                                                         
[INFO]                                                                            
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile)  custom-archetype ---
[INFO] No sources to compile   
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ custom-archetype 
---                                                                              
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ custom-archetype ---
[INFO] --- maven-install-plugin:2.4:install (default-install) @ custom-archetype        
 ---
[INFO] Installing D:\dev\customarchetype\target\custom-archetype-0.0.1-SNAPSHOT.jar to C:\Users\xxxx\.m2\repository\uk\co\xxxxx\custom-archetype\0
.0.1-SNAPSHOT\custom-archetype-0.0.1-SNAPSHOT.jar                                
[INFO] Installing D:\dev\custom\customarchetype\pom.xml to C:\Users\xxxx\.m2\rep
ository\uk\co\xxxxx\custom-archetype\0.0.2-SNAPSHOT\custom-archetype-0.0.
1-SNAPSHOT.pom                                                                    
[INFO] ------------------------------------------------------------------------   
[INFO] BUILD SUCCESS                                                              
[INFO] ------------------------------------------------------------------------   
[INFO] Total time: 2.849 s                                                        
[INFO] Finished at: 2019-01-11T09:29:18+00:00                                     
[INFO] Final Memory: 8M/245M                                                      
[INFO] ------------------------------------------------------------------------   

我正在运行以下 maven 命令来生成我的项目

mvn archetype:generate -DarchetypeGroupId=uk.co.gamma.xxxxx -DarchetypeArtifactId=custom-archetype -DarchetypeVersion=0.0.1-SNAPSHOT -DgroupId=uk.co.xxxxx -Dversion=0.0.1-SNAPSHOT -DartifactId=test12 -Dpackage=uk.co.xxxxx

这是输出

[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Archetype repository not defined. Using the one from [uk.co.xxxxx:custom-archetype:0.0.1-SNAPSHOT] found in catalog local
[INFO] Using property: groupId = uk.co.xxxxx
[INFO] Using property: artifactId = test12
[INFO] Using property: version = 0.0.1-SNAPSHOT
[INFO] Using property: package = uk.co.xxxxx
[INFO] Using property: thorntail-version = 2.2.1.Final
Confirm properties configuration:
groupId: uk.co.xxxxx
artifactId: test12
version: 0.0.1-SNAPSHOT
package: uk.co.xxxxx
thorntail-version: 2.2.1.Final
 Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: custom-archetype:0.0.1-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: uk.co.xxxxx
[INFO] Parameter: artifactId, Value: test12
[INFO] Parameter: version, Value: 0.0.1-SNAPSHOT
[INFO] Parameter: package, Value: uk.co.xxxxx
[INFO] Parameter: packageInPathFormat, Value: uk/co/xxxxx
[INFO] Parameter: package, Value: uk.co.xxxxx
[INFO] Parameter: version, Value: 0.0.1-SNAPSHOT
[INFO] Parameter: thorntail-version, Value: 2.2.1.Final
[INFO] Parameter: groupId, Value: uk.co.xxxxx
[INFO] Parameter: artifactId, Value: test12
[INFO] Project created from Archetype in dir: D:\dev\testarchetype\test12
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.281 s
[INFO] Finished at: 2019-01-11T10:27:45+00:00
[INFO] Final Memory: 13M/183M
[INFO] ------------------------------------------------------------------------

生成的项目只包含 pom.xml。

从该原型生成项目时,我需要做什么才能包含这些文件?

【问题讨论】:

  • archetype.xml 是否“有效”?在您的帖子中似乎无效(关闭&lt;/archetype&gt;)。尽管如此,对“mvn(调试)日志”的进一步了解还是会有所帮助的。 ..当然还有pom ... :)
  • 添加了 pom 并更正了 archtype.xml。我不知道我应该从日志中复制什么而不只是贴一堵墙。当我构建原型项目或从原型生成项目时,没有警告或错误。
  • 你说“我的 pom”,你应该说“哪一个?”;) 来自链接:“-原型 pom (pom.xml in: src/main/resources/archetype-resources) - 原型的 pom(原型根目录中的 pom.xml)。”,也许这已经是您监督的步骤....如果有其他事情,请发布“mvn -D install”的输出。
  • 那是根 pom。我的项目中确实有两个 pom,并且在使用此原型时可以正确生成原型 pom。
  • 欢迎,兄弟!很高兴,你找到了! :)(考虑发布答案(并接受))

标签: java maven maven-archetype


【解决方案1】:

archetype-metadata.xml 是原型 2.x 描述符,而 archetype.xml 是原型 1.x 描述符。使用这两者意味着 archetype.xml 中的任何内容都将被忽略。在 archetype-metadata.xml 中包含文件生成并删除 archetype.xml 可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 2016-11-20
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多