简体   繁体   English

在同一目录中使用Maven Archetype Generate

[英]Using Maven Archetype Generate in the same Directory

Is there a way to run mvn archetype:generate and target the current directory instead of creating a directory from the artifactId? 有没有办法运行mvn archetype:生成并定位当前目录而不是从artifactId创建目录? The plugin supposedly takes a basedir parameter, but passing in "-Dbasedir=." 该插件应该采用一个basedir参数,但传入“-Dbasedir =”。 doesn't do the trick. 没有办法。

For additional context, I've got 2 Git repositories setup. 对于其他上下文,我有2个Git存储库设置。 The first contains the source of a Maven archetype for generating custom web services, and the second is a sample service generated from the archetype. 第一个包含用于生成自定义Web服务的Maven原型的源,第二个是从原型生成的示例服务。 I've got a Jenkins job that builds my archetype by essentially just running "mvn clean install". 我有一个Jenkins工作,通过运行“mvn clean install”来构建我的原型。 I'm trying to setup a second Jenkins job as part of our CI workflow generates a test service using "mvn archetype:generate", builds the service with mvn clean install, spins up the service, runs some integration tests, and then pushes the source for the test service into a second repository if the tests pass. 我正在尝试设置第二个Jenkins作业,作为CI工作流的一部分,使用“mvn archetype:generate”生成测试服务,使用mvn clean install构建服务,旋转服务,运行一些集成测试,然后推送如果测试通过,测试服务的源代码到第二个存储库。 Both Jenkins jobs use the Maven 2/3 build job type , and I've specified our Git repo information in the SCM section of the job configuration, so the jobs start by doing a "git clone". 两个Jenkins作业都使用Maven 2/3构建作业类型 ,并且我在作业配置的SCM部分中指定了我们的Git repo信息,因此作业首先执行“git clone”。

For the second job, my current workflow looks like this: 对于第二项工作,我当前的工作流程如下所示:

// Clean out the existing source and commit locally.
git rm -r . 
git commit -m "Cleaning out previous version." .

// Generate the new source from the archetype.
mvn archetype:generate ...

// Big hack that I'd like to remove.
mv <artifactId>/* .
rm -rf <artifactId>

// Add the new generated source and commit locally.
git add -A .
git commit -m "Committing new version." .

// Build and test.
mvn integration-test

// Assuming that passed, commit the changes.
git push origin master

The hack is there because I'm not sure how to tell the archetype plugin to use my current directory instead of creating a nested directory with the name of the artifactId, so I have to move everything back into my local repository root directory and delete the directory that was created. 黑客是因为我不知道如何告诉archetype插件使用我当前的目录而不是创建一个带有artifactId名称的嵌套目录,所以我必须将所有内容移回我的本地存储库根目录并删除创建的目录。 Not the end of the world, but it feels ugly. 不是世界末日,但感觉很难看。 Note that I'm open to suggestions on how to better accomplish my overall goal in addition to answers to my initial question. 请注意,除了我最初的问题的答案之外,我愿意接受有关如何更好地实现我的总体目标的建议。 :) Thanks in advance! :) 提前致谢!

It is not possible without some kind of workaround, because of 2 reasons: 没有某种解决方法是不可能的,因为有两个原因:

  1. The basedir property doesn't work, see this issue . basedir属性不起作用,请参阅此问题

  2. The archetype plugin always adds artifactId as the last child directory. archetype插件总是将artifactId添加为最后一个子目录。 You can see this in the sources . 你可以在消息来源中看到这一点。

Your options 你的选择

  • keep your hack, it is not so horrible ;-) 保持你的黑客,它不是那么可怕;-)

  • is it important that the project is in the root of the repository? 项目是否在存储库的根目录中是否重要? if not just do cd <artifactId> before the build/integration tests 如果不是在构建/集成测试之前做cd <artifactId>

  • if building from root of the repository is required then use root pom.xml of type pom, with the generated project as child module (I think this is what other post suggests as #3) 如果需要从存储库的根目录构建,那么使用pom类型的root pom.xml,将生成的项目作为子模块(我认为这是其他帖子建议的#3)

  • provide a PR to add desired functionality to maven archetype plugin, this might take some time, but is probably the cleanest solution 提供一个PR来为maven archetype插件添加所需的功能,这可能需要一些时间,但可能是最干净的解决方案

I have three ideas: 我有三个想法:

  1. You can try to use an absolute path for basedir but I think the property is overwritten by Maven when it sees a pom.xml file. 您可以尝试使用basedir的绝对路径,但我认为Maven在看到pom.xml文件时会覆盖该属性。

  2. Use a script and pushd .. to change to the parent folder before you run mvn archetype:generate . 在运行mvn archetype:generate之前,使用脚本并pushd ..更改为父文件夹。 popd will get you back. popd会让你回来。

  3. Use a maven module build. 使用maven模块构建。 That way, you have a root POM which builds the archetype and you can run mvn archetype:generate to generate a module inside of the project: 这样,你有一个构建原型的根POM,你可以运行mvn archetype:generate来生成项目内的模块:

      demo-project/ pom.xml archetype/ pom.xml 

I know it's old topic but I had similar issue: 我知道这是一个古老的话题,但我有类似的问题:

The archetype:generate goal has a parameter outputDirectory, you can set it to root folder (../). 原型:generate goal有一个参数outputDirectory,你可以将它设置为根文件夹(../)。 When generating artifact from archetype it's important that your artifact name is same as current directory. 从原型生成工件时,您的工件名称与当前目录相同很重要。

mvn archetype:generate -DarchetypeGroupId=[archetype-group] -DarchetypeArtifactId=[archetype-id] -DoutputDirectory=../ -DartifactId=[current-folder-name] -DgroupId=[whatever-you-want] -Dversion=[ex: 0.1] -Dpackage=[jar|pom|etc.] -B

I don't have the reputation to comment so I'll have to make an answer instead. 我没有评论的声誉所以我将不得不做出回答。 It's not a good idea to have these lines in your script: 在脚本中包含这些行并不是一个好主意:

mv <artifactId>/* .
rm -rf <artifactId>

If someone decides to fill in an empty (or just '/') artifactId you end up trying to move the whole disk into this directory, only to then delete the entire disk. 如果有人决定填写一个空的(或只是'/')artifactId,你最终会尝试将整个磁盘移动到这个目录中,然后才删除整个磁盘。 Instead you can do this: 相反,你可以这样做:

mv ./<artifactId>/* .
rmdir ./<artifactId>

The move can't hurt and the rmdir can only delete empty directories. 移动不会受到伤害,rmdir只能删除空目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM