简体   繁体   English

如何为一组目标创建 Maven 别名?

[英]How create maven alias for set of goals?

I'm configuring a Maven project and want to be able to alias, like我正在配置一个 Maven 项目并希望能够使用别名,例如

mvn server - to execute mvn clean package tomcat:run mvn server - 执行mvn clean package tomcat:run

The Grunt task runner does it very well, but I haven't found a way to do the same in Maven. Grunt 任务运行器做得很好,但我还没有找到在 Maven 中做同样事情的方法。 Is it possible?可能吗?

You can define a <defaultGoal>...</defaultGoal> in your pom if you like.如果你愿意,你可以在你的 pom 中定义一个<defaultGoal>...</defaultGoal> So you can define something like this:所以你可以定义这样的东西:

<project>
  <build>
    <defaultGoal>clean package tomcat:run</defaultGoal>
    .
  </build>
</project>

will be activated if you simply call mvn ...not really an alias, cause usually you don't need one...如果你简单地调用mvn将被激活......不是真正的别名,因为通常你不需要一个......

Out of the box I don't know of any solution that doesn't imply using a plugin.开箱即用我不知道任何不暗示使用插件的解决方案。 A simple solution may be adding aliases to your .bashrc file in your home directory (for Linux) or .bash_profile (on OS X) for your desired instructions.一个简单的解决方案可能是为您的主目录(对于 Linux)或.bash_profile (对于 OS X)中的.bashrc文件添加别名,以获得所需的说明。

Eg: Adding a line alias my-alias="mvn clean install" will allow you to execute the command my-alias in the terminal, obtaining the same result as running the mvn clean install instruction itself.例如:添加一行alias my-alias="mvn clean install"将允许您在终端中执行命令my-alias ,获得与运行mvn clean install指令本身相同的结果。 Add another line alias my-alias-port="mvn clean install -Dcrx.port=9200" for a second instruction, and so on.添加另一行alias my-alias-port="mvn clean install -Dcrx.port=9200"作为第二条指令,依此类推。

Optionally, you can execute alias to see a list of all your aliases and their respective instructions.或者,您可以执行alias查看所有别名的列表及其各自的说明。

The best solution I have found to this is to use a combination of:我发现的最佳解决方案是使用以下组合:

Roughly in that order of preference.大致按照这个优先顺序。

GNU Make is especially nice because it offers bash completion. GNU Make 特别好,因为它提供了 bash 补全。

An example Makefile for your specific example would be:您的特定示例的示例 Makefile 将是:

.PHONEY: server

server:
[tab]mvn clean package tomcat:run

Replace [tab] with a real tab!将 [tab] 替换为真正的选项卡! See make documentation.请参阅制作文档。

Then you can run:然后你可以运行:

make server

For windows environments you will need to install cygwin or something equivalent.对于 Windows 环境,您需要安装 cygwin 或类似的东西。 For Mac you don't have to but you should probably install homebrew.对于 Mac,您不必安装,但您可能应该安装 homebrew。

Finally the Maven Bash completion albeit doesn't do aliases but will greatly facilitate typing maven commands (press tab).最后, Maven Bash 完成虽然不做别名,但会极大地方便输入 Maven 命令(按 Tab)。 Many package managers have this as a package (ie homebrew has it as maven-completion).许多包管理器将其作为一个包(即自制程序将其作为 maven-completion)。

You can also add the following function to your .bashrc file:您还可以将以下函数添加到.bashrc文件中:

function mvn() {
  if [ "$1" = "i" ]; then
    command mvn install
  else
    command mvn $@
  fi
}

And so you can invoke the mvn install with the mvn i alias.因此,您可以使用mvn i别名调用mvn install

Everything else that is not mvn i will call the original mvn command instead.其他不是mvn i都会调用原始的mvn命令。

Alias-maven-plugin is what you are looking for. Alias-maven-plugin是您正在寻找的。

Following the site:关注网站:

Whenever you type a command in a shell, for instance例如,每当您在 shell 中键入命令时

mvn clean install

you could spare time in simply using an alias like this您可以花时间简单地使用这样的别名

i

It has also more advantages - you could configure plugin by XML file.它还有更多的优点——你可以通过 XML 文件配置插件。

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

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