Bundle & Minify 在 maven web 项目中不起作用

Bundle & Minify not working in maven web project

我正在尝试 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

静态-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.

我正在分享替代插件,它解决了我的目的,因为以前的插件(见问题)既不适合我,也没有收到任何关于这里和 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

如果有人仍然对问题中提到的我以前的插件有解决方案,请post您的回答。