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

发布于 2024-08-28 17:32:20 字数 614 浏览 13 评论 0原文

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

但现在我想通过使用 Eclipse 和 m2eclipse 插件来使开发变得更容易。我希望 Eclipse 在我更改 JavaScript 文件时调用编译目标。我该怎么做?当我保存 JavaScript 文件时,我只会在 Maven 控制台中看到“AUTO_BUILD”日志行,仅此而已。

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

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

有什么提示吗?

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.

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.

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.

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?

Any hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铜锣湾横着走 2024-09-04 17:32:20

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

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

当我告诉 Eclipse“清理”我的项目时它也起作用。然后调用编译目标target。

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


正如我所说,我只是将 js 文件放在 resources 下。但也许还有另一种选择:向项目中添加另一个构建器。 右键单击您的项目,然后属性>建设者>新的> Maven 构建并将您的插件目标定义为在自动构建目标期间运行的目标(更改或删除其他目标以满足您的需求):

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

但我更喜欢另一种方法。

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.

As you've noticed, the default goals run on incremental builds in Eclipse are process-resources and 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.

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

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


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-click on your project, then Properties > Builders > New > Maven Build and define your plugin goal as goal to run during Auto Build Goals (change or remove the other goals to suit your needs):

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

But I prefer the other approach.

南街女流氓 2024-09-04 17:32:20

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

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

maven 安装插件自动部署在本地的 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>

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

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 :

You have to add a valid maven lifecycle action

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

<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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文