简体   繁体   English

将纯RequireJS项目移至Maven

[英]Move pure RequireJS project to Maven

I have project that use bare Javascript (+ Jquery + Backbone) with RequireJS. 我有一个使用RequireJS的裸露Javascript(+ Jquery + Backbone)的项目。 I include dependencies like: 我包括类似的依赖项:

require.config({
    baseUrl: "./js/",
    paths: {
        jquery: 'lib/jquery-1.8.2',
        underscore: 'lib/underscore-1.4.2',
        backbone: 'lib/backbone-1.1.2',
        'backbone.localStorage': 'lib/backbone.localStorage'
    },
    shim: {
        underscore: {
            exports: "_"
        },
        backbone: {
            deps: ['underscore', 'jquery'],
            exports: 'Backbone'
        },
        'backbone.localStorage': {
            deps: ['backbone'],
            exports: 'Backbone'
        }
    }
});

I want: move project to maven. 我想要:将项目移至Maven。 I don't want to include all that deps in such ugly way. 我不想以如此丑陋的方式包括所有这些部门。 Moreover, I want to use Hibenate, Spring and another deps that should be imported via maven. 此外,我想使用Hibenate,Spring和其他应该通过Maven导入的deps。

Problem: I haven't deep understanding of how do maven plugins or RequireJs work. 问题:我对maven插件或RequireJs的工作方式尚未深入了解。 I looked at this , but understand nothing. 我看着这个 ,但一无所知。

Question: Can you write a step-by-step action points how to move my app to maven? 问题: 您能否编写一个逐步的操作要点,说明如何将我的应用程序移至Maven?

Maven is not really an answer to the ( alleged ) "ugliness" of the configuration you showed because it works on a completely different level. Maven并不是对您显示的( 所谓的 )“丑陋”配置的真正答案,因为它在完全不同的层次上工作。 To begin with, Maven is a server-side tool and RequireJS is client-side (apologies if you already knew it, but -> "I haven't deep understanding of how do maven plugins or RequireJs work" ). 首先,Maven是服务器端工具,而RequireJS是客户端(很抱歉,如果您已经知道的话,但是-> “我对maven插件或RequireJs的工作方式尚未深入了解” )。 This configuration is specific to RequireJS so there's no way to go around it. 此配置特定于RequireJS,因此无法解决。

Even with Maven you still need to configure your front-end application and tell it where to find the required dependencies and how to initialise them. 即使使用Maven,您仍然需要配置您的前端应用程序,并告诉它在哪里可以找到所需的依赖项以及如何对其进行初始化。 Client application's paths are web resources which can be arbitrarily different from the file structure of your project so it's not easy to automate the configuration process. 客户端应用程序的路径是Web资源,可以与项目的文件结构任意不同,因此自动化配置过程并不容易。

I've been using the RequireJS maven plugin (found it in one of the answers in the same question you posted) for wrapping r.js but it was rather painful to set up, especially with resource timestamping. 我一直在使用RequireJS maven插件 (在您发布的同一问题的答案之一中找到了它)来包装r.js但设置起来特别r.js ,尤其是在带有时间戳的情况下。 I just feel that this approach is flawed due to fundamental differences between Java and JavaScript tooling - you'll always end up with a less-than-ideal compromise and any non-standard steps will eat a lot of time and research. 我只是觉得这种方法是有缺陷的,这是由于Java和JavaScript工具之间的根本差异所致-您总是会得到不理想的折衷,并且任何非标准的步骤都将花费大量时间和精力。

Another approach that's gaining popularity is to completely split your client- and server-side applications into separate projects, allowing you to use language-specific tooling to make the process as smooth as possible. 越来越流行的另一种方法是将客户端和服务器端应用程序完全拆分为单独的项目,从而允许您使用特定于语言的工具来使过程尽可能地流畅。 Server-side uses Maven, client-side uses Grunt+Bower+RequireJS+etc. 服务器端使用Maven,客户端使用Grunt + Bower + RequireJS + etc。 Both sides talk through well-defined interfaces using JSON over HTTP. 双方使用基于HTTP的JSON通过定义明确的接口进行对话。

It should be easy to find some introductory reading material, but since you're mentioning Spring it could be worth starting by having a look at the reference Spring application: Sagan . 找到一些入门阅读材料应该很容易,但是由于您提到的是Spring,因此值得一看一下参考Spring应用程序Sagan It demonstrates how to split front- and back-end into separate Maven submodules that share the same common "core". 它演示了如何将前端和后端拆分为共享相同公共“核心”的单独的Maven子模块。


A stop-gap solution that makes working with JS libraries with Maven a little bit more civilised is using webjars . 使用webjars的权宜之计解决方案使使用Maven JS库更加文明。 You define your dependencies directly in pom.xml and they're easily exposed to the front-end like other static assets. 您可以直接在pom.xml中定义依赖项,就像其他静态资产一样,它们很容易暴露在前端。 However, this limits your options when it comes to bundling or customisation of the libraries. 但是,这在绑定或自定义库时会限制您的选择。

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

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