如何在 Eclipse 中保存时启动 Maven“编译”目标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2632425/
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-09-10 19:19:47  来源:igfitidea点击:

How do I start Maven "compile" goal on save in Eclipse?

eclipsemaven-2m2eclipse

提问by kayahr

I have a Maven project with JavaScript code. There is a special javascript compiler plugin connected to the compile goal in the pom.xml. So when I type "mvn compile" then the JavaScript sources in src/main/javascript are compiled (compressed and obfuscated) and saved into the target/classes directory. On the command line this works great.

我有一个带有 JavaScript 代码的 Maven 项目。有一个特殊的 javascript 编译器插件连接到 pom.xml 中的 compile 目标。因此,当我输入“mvn compile”时,src/main/javascript 中的 JavaScript 源代码将被编译(压缩和混淆)并保存到 target/classes 目录中。在命令行上,这很好用。

But now I want to make the development easier by using Eclipse with the m2eclipse plugin. I want Eclipse to call the compile goal whenever I change a JavaScript file. How can I do this? When I save a JavaScript file then I just see a "AUTO_BUILD" logging line in the maven console and that's it.

但是现在我想通过使用带有 m2eclipse 插件的 Eclipse 来使开发更容易。我希望 Eclipse 在我更改 JavaScript 文件时调用编译目标。我怎样才能做到这一点?当我保存 JavaScript 文件时,我只会在 Maven 控制台中看到“AUTO_BUILD”日志记录行,仅此而已。

In the project preferences it is possible to configure a lifecycle mapping. But for some reason I can only add custom goals to "after clean" and "on resource changed". When I add the "compile" goal to the "resource changed" lifecycle mapping, then the JavaScript files are compiled when I change a resource. So I could put my JavaScript files into the resources folder instead and it would work but this sounds pretty ugly.

在项目首选项中,可以配置生命周期映射。但出于某种原因,我只能将自定义目标添加到“清理后”和“资源更改时”。当我将“编译”目标添加到“资源已更改”生命周期映射时,JavaScript 文件将在我更改资源时进行编译。所以我可以把我的 JavaScript 文件放到资源文件夹中,它会工作,但这听起来很丑陋。

It is also working when I tell Eclipse to "clean" my project. Then the compile goal target is called. So the functionality is all there I just want to have it executed when I save a JavaScript file. This must be possible somehow, or not?

当我告诉 Eclipse“清理”我的项目时,它也能正常工作。然后调用编译目标目标。所以功能就在那里,我只想在保存 JavaScript 文件时执行它。这一定是可能的,还是不可能?

Any hints?

任何提示?

采纳答案by Pascal Thivent

In the project preferences it is possible to configure a lifecycle mapping. But for some reason I can only add custom goals to "after clean" and "on resource changed". When I add the "compile" goal to the "resource changed" lifecycle mapping, then the JavaScript files are compiled when I change a resource. So I could put my JavaScript files into the resources folder instead and it would work but this sounds pretty ugly.

在项目首选项中,可以配置生命周期映射。但出于某种原因,我只能将自定义目标添加到“清理后”和“资源更改时”。当我将“编译”目标添加到“资源已更改”生命周期映射时,JavaScript 文件将在我更改资源时进行编译。所以我可以把我的 JavaScript 文件放到资源文件夹中,它会工作,但这听起来很丑陋。

As you've noticed, the default goals run on incremental builds in Eclipse are process-resourcesand resources:testResources. Personally, I don't find ugly to put js file under resources and I would just bind the javascript plugin on process-resources.

正如您所注意到的,在 Eclipse 中运行增量构建的默认目标是process-resourcesresources:testResources。就我个人而言,我认为将 js 文件放在资源下并不难看,我只是将 javascript 插件绑定到process-resources.

It is also working when I tell Eclipse to "clean" my project. Then the compile goal target is called.

当我告诉 Eclipse“清理”我的项目时,它也能正常工作。然后调用编译目标目标。

On full build (after a clean from Eclipse), goals run are process-test-resourceswhich is actually a build lifecycle phase that includes the compilephase, that's why compileget called when you clean your project from Eclipse. But this doesn't solve your issue (running your plugin on save).

在完整构建中(在从 Eclipse 中清理之后),运行的目标process-test-resources实际上是一个包含该compile阶段的构建生命周期阶段,这就是compile当您从 Eclipse 中清理项目时被调用的原因。但这并不能解决您的问题(在保存时运行您的插件)。



As I said, I would just put the js file under resources. But there is maybe another option: adding another Builder to the project. Right-clickon your project, then Properties > Builders > New > Maven Buildand define your plugin goal as goal to run during Auto Build Goals(change or remove the other goals to suit your needs):

正如我所说,我只是将 js 文件放在资源下。但也许还有另一种选择:向项目添加另一个 Builder。右键单击您的项目,然后Properties > Builders > New > Maven Build并将您的插件目标定义为在Auto Build 目标期间运行的目标(更改或删除其他目标以满足您的需要):

alt text http://img694.imageshack.us/img694/2382/screenshot003wo.png

替代文字 http://img694.imageshack.us/img694/2382/screenshot003wo.png

But I prefer the other approach.

但我更喜欢另一种方法。

回答by childno?.de

since @pascal-thivent answer is outdated (as mentioned in the comments) and hinted by @akostadinov https://www.eclipse.org/m2e/documentation/m2e-execution-not-covered.html:

因为@pascal-thivent 答案已经过时(如评论中所述)并由@akostadinov https://www.eclipse.org/m2e/documentation/m2e-execution-not-covered.html暗示:

You have to add a valid maven lifecycle action

您必须添加有效的 Maven 生命周期操作

Example for a jar which is automatically deployed locally by maven install plugin:

maven install 插件在本地自动部署的 jar 示例

<build>
    <!-- ... -->

    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>

                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-jar-plugin</artifactId>
                                    <versionRange>[2.0,)</versionRange>
                                    <goals>
                                        <goal>jar</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <execute>
                                        <runOnConfiguration>true</runOnConfiguration>
                                        <runOnIncremental>true</runOnIncremental>
                                    </execute>
                                </action>
                            </pluginExecution>

                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-install-plugin</artifactId>
                                    <versionRange>[2.5.0,)</versionRange>
                                    <goals>
                                        <goal>install</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <execute>
                                        <runOnConfiguration>true</runOnConfiguration>
                                        <runOnIncremental>true</runOnIncremental>
                                    </execute>
                                </action>
                            </pluginExecution>

                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Hint: relates to Maven Project Builder is invoked every time I change a source file (GWT)and as a warning: install typically includes tests if you have included them in your normal maven build cycle

提示:每次更改源文件 (GWT) 时都会调用 Maven Project Builder并作为警告:安装通常包括测试,如果您已将它们包含在正常的 Maven 构建周期中