java 关于如何创建新的 Maven 原型的任何好的高级指南?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4795515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:03:54  来源:igfitidea点击:

Any good advanced guides on how to create new maven archetypes?

javamavenarchetypes

提问by menapole

I'm looking for a guide on how to create new maven archetypes that involve using parameters to create directories and file names where the parameters are used as prefixes in the file names and part of the package structure/directories that are created by the archetype.

我正在寻找有关如何创建新 maven 原型的指南,其中涉及使用参数来创建目录和文件名,其中参数用作文件名中的前缀以及由原型创建的包结构/目录的一部分。

All I can find is very simple instructions on how to make very simple projects.

我所能找到的只是关于如何制作非常简单的项目的非常简单的说明。

采纳答案by fmucar

回答by menapole

I think I found the a site that helps me a little more once I figured out that Velocity templates are used as part of the archetype processes.

一旦我发现 Velocity 模板被用作原型过程的一部分,我想我找到了一个对我有更多帮助的网站。

HowToCreateMavenArchetypeFromProject

HowToCreateMavenArchetypeFromProject

回答by Frank Zhang

I think we met the same problem, finally i resolved it with enlightening from this article

我想我们遇到了同样的问题,最后我通过这篇文章的启发解决了它

assuming you want to generate package com.company.base.dal.dao, we call com.company.baseis root package, dal.daois core package, then just put your template java file under src/main/java/dal/dao, and tweak the packaged="true"in META-INF/maven/archetype-metadata.xml as below

假设您要生成包com.company.basedal.dao,我们称com.company.base是根包,dal.dao是核心包,然后把你的模板 java 文件放在 src/main/java/dal/dao 下,并packaged="true"在 META-INF/maven/ 中调整archetype-metadata.xml 如下

            <fileSet filtered="true" packaged="true" encoding="GBK"><!--packaged="true" tells maven to copy the core package in to root package while creating a project.-->
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </fileSet>

say, if you set -Dpackage=com.alibaba.chinaafter execute mvn archetype:generate, it will create java source package as com/alibaba/china/dal/dao/sample.java

比如说,如果你-Dpackage=com.alibaba.china在执行 mvn archetype:generate 后设置,它会创建 java 源包为 com/alibaba/china/dal/dao/sample.java

as how to give the prefix package information in sample.java, just use ${package} as below

至于如何在sample.java中给出前缀包信息,只需使用${package}如下

package ${package}.dal.dao;

the archetype plugin will replace ${package} with -Dpackage as velocity template

原型插件将用 -Dpackage 替换 ${package} 作为速度模板

回答by Jesus M C

from the maven documentacion: http://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html

来自 Maven 文档:http: //maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html

A fileset defines the way the project's files located in the jar file are used by the Archetype Plugin to generate a project. If file or directory name contains propertypattern, it is replaced with corresponding property value.

文件集定义了 Archetype 插件使用位于 jar 文件中的项目文件来生成项目的方式。如果文件或目录名称包含属性模式,则将其替换为相应的属性值。

if you can name the file as

如果您可以将文件命名为

__artifactId___local.properties

__artifactId___local.properties

... when you generate the proyect with artifactId=foo it will contains this file:

...当您使用 artifactId=foo 生成项目时,它将包含以下文件:

foo_local.properties

foo_local.properties

回答by Tarun

Even I am looking for the same thing. Even I find Maven home page very basic and does not fulfill our requirement.

甚至我也在寻找同样的东西。即使我发现 Maven 主页非常基本并且不能满足我们的要求。

I even tried HowToCreateMavenArchetypeFromProjectBut I still have one question. How to invoke a specific file of the archetype when the project of that type of archetype is generated. To be specific, if I have a java class App.java how to invoke that while creating project?

我什至尝试过HowToCreateMavenArchetypeFromProject但我仍然有一个问题。生成该类型原型的项目时如何调用该原型的特定文件。具体来说,如果我有一个 java 类 App.java 如何在创建项目时调用它?

What happens with create-archetype-from-project is it creates a project of the same files and directory structure and we need to run that again to get that in action.

create-archetype-from-project 发生的事情是它创建了一个具有相同文件和目录结构的项目,我们需要再次运行它以使其生效。

Also, ${groupId} and such stuff work only in maven files. What if I have a property file and wanna use the artifactId the user has entered?

此外, ${groupId} 和此类内容仅适用于 Maven 文件。如果我有一个属性文件并想使用用户输入的 artifactId 怎么办?

Do let me know if you come across any good stuff. Meanwhile, I will also let you know when I find any solution.

如果您遇到任何好东西,请告诉我。同时,当我找到任何解决方案时,我也会通知您。

Thanks

谢谢

回答by ben rhouma moez