【问题标题】:Bundle & Minify not working in maven web project捆绑和缩小在 Maven Web 项目中不起作用
【发布时间】:2018-10-22 09:08:47
【问题描述】:

我正在我的示例项目中尝试this plugin(以优化性能)将所有 CSS 捆绑到一个 CSS 中,并将所有 JS 捆绑到一个具有缩小版本的 JS 中,但是在清理和构建后项目结构保持不变。一切都没有像预期的那样发生变化。

我也在Github提出了类似的票,但没有收到任何更新。

请找到我的项目结构:

├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── darshan
    │   │           └── SourceMapFilter.java
    │   ├── resources
    │   │   ├── readme.txt
    │   │   └── static-bundles.json
    │   └── webapp
    │       ├── css
    │       │   ├── custom.css
    │       │   └── style.css
    │       ├── index.html
    │       ├── js
    │       │   ├── custom.js
    │       │   └── script.js
    │       ├── META-INF
    │       │   └── context.xml
    │       └── WEB-INF
    │           └── web.xml
    └── test
        └── java

static-bundles.json:

{
    "bundles": [
        {
            "type": "css",
            "name": "static-combined.css",
            "files": [
                "custom.css",
                "style.css"
            ]
        },
        {
            "type": "js",
            "name": "static-combined.js",
            "files": [
                "custom.js",
                "script.js"
            ]
        }
    ]
}

pom.xml 插件配置:

<plugin>
    <groupId>com.samaxes.maven</groupId>
    <artifactId>minify-maven-plugin</artifactId>
    <version>1.7.6</version>
    <executions>
        <execution>
            <id>bundle-minify</id>
            <phase>package</phase>
            <goals>
                <goal>minify</goal>
            </goals>
            <configuration>
                <webappSourceDir>${project.basedir}</webappSourceDir>
                <webappTargetDir>${project.basedir}</webappTargetDir>

                <cssSourceDir>css</cssSourceDir>
                <cssSourceFiles>
                    <cssSourceFile>custom.css</cssSourceFile>
                    <cssSourceFile>style.css</cssSourceFile>
                </cssSourceFiles>
                <cssTargetDir>css</cssTargetDir>
                <cssFinalFile>static-combined.css</cssFinalFile>
                <cssSourceDir>js</cssSourceDir>
                <jsSourceFiles>
                    <jsSourceFile>custom.js</jsSourceFile>
                    <jsSourceFile>script.js</jsSourceFile>
                </jsSourceFiles>
                <jsTargetDir>js</jsTargetDir>
                <jsFinalFile>static-combined.js</jsFinalFile>
            </configuration>
        </execution>
    </executions>
</plugin>

我已经尝试过使用绝对路径,但也没有运气。使用 JDK 1.8。

【问题讨论】:

  • “不工作”是对问题的错误描述...
  • 更新问题。希望对您有所帮助。

标签: java maven jakarta-ee bundle minify


【解决方案1】:

我正在分享替代插件,这解决了我的目的,因为以前的插件(请参阅问题)既不适合我,也不在此处和 github 上收到任何更新。

在您的pom.xml 中添加以下插件。

<plugin>
    <groupId>com.github.kospiotr</groupId>
    <artifactId>bundler-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>js</id>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <verbose>true</verbose>
                <munge>false</munge>
                <inputFilePah>${project.basedir}/src/main/webapp/index-dev.html</inputFilePah>
                <outputFilePath>${project.build.directory}/${project.build.finalName}/index.html</outputFilePath>
            </configuration>
        </execution>
    </executions>
</plugin>

请注意,不需要 index.html。它会自动生成。

index-dev.html:(请注意,捆绑注释是强制性的)

<!-- bundle:css app-#hash#.min.css-->
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<link href="css/custom.css" rel="stylesheet" type="text/css"/>
<!-- /bundle -->

<!-- bundle:js app-#hash#.min.js-->
<script src="js/custom.js"></script>
<script src="js/script.js"></script>
<!-- /bundle -->

生成的 index.html

<link rel="stylesheet" href="app-d3c9aea5a76e300e113c07b3717683b3.min.css"/>
<script src="app-f1b7efa7214d328d11623c0f4b3efb19.min.js"></script>

输出结构

.
├── app-d3c9aea5a76e300e113c07b3717683b3.min.css
├── app-f1b7efa7214d328d11623c0f4b3efb19.min.js
├── css
│   ├── custom.css
│   └── style.css
├── index-dev.html
├── index.html
├── js
│   ├── app.js
│   ├── custom.js
│   └── script.js
├── META-INF
│   └── context.xml
└── WEB-INF
    ├── classes
    │   ├── com
    │   │   └── darshan
    │   │       └── SourceMapFilter.class
    │   ├── readme.txt
    │   └── static-bundles.json
    └── web.xml

我的 github 项目:https://github.com/darsh9292/bundle-web-app

如果有人仍然对我之前提到的插件有解决方案,请发布您的答案。

【讨论】:

    猜你喜欢
    • 2018-04-18
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 2014-11-26
    • 2013-06-27
    相关资源
    最近更新 更多