2016-01-13 52 views
9

我将使用maven依赖项com.samaxes.maven#minify-maven-plugin来缩小我的前端项目。这个前端项目打包成一个jar(java归档) - 所以我没有war文件,因为我使用了Spring引导。将缩小文件放入罐中

使用底部的配置* .min.js和* .min.css文件生成,但它们不会陷入jar文件。我已经阅读了一些线程,并尝试了一些东西,但没有成功:在JAR文件中,仍然存在unminified的css和js文件。

有没有人有任何提示我可以做什么。我的项目中有很多css和js文件在不同的文件夹结构中,并且缩小的文件应该放在与未修剪的文件相同的位置。

<build> 
<plugins> 
    <plugin> 
    <groupId>com.samaxes.maven</groupId> 
    <artifactId>minify-maven-plugin</artifactId> 
    <version>1.7.4</version> 
    <executions> 
     <execution> 
     <id>default-minify</id> 
     <phase>package</phase> 
     <configuration> 
      <charset>UTF-8</charset> 
      <skipMerge>true</skipMerge> 
      <nosuffix>true</nosuffix> 
      <closureCompilationLevel>WHITESPACE_ONLY</closureCompilationLevel> 
      <webappSourceDir>src/main/resources</webappSourceDir> 
      <cssSourceDir>./</cssSourceDir> 
      <cssTargetDir>./minified</cssTargetDir> 
      <cssSourceIncludes> 
      <cssSourceInclude>**/*.css</cssSourceInclude> 
      </cssSourceIncludes> 
      <cssSourceExcludes> 
      <cssSourceExclude>**/*.min.css</cssSourceExclude> 
      </cssSourceExcludes> 
      <jsSourceIncludes> 
      <jsSourceInclude>**/*.js</jsSourceInclude> 
      </jsSourceIncludes> 
      <jsSourceExcludes> 
      <jsSourceExclude>**/*.min.js</jsSourceExclude> 
      </jsSourceExcludes> 
      <jsEngine>CLOSURE</jsEngine> 
     </configuration> 
     <goals> 
      <goal>minify</goal> 
     </goals> 
     </execution> 
    </executions> 
    </plugin> 
</plugins> 

[编辑]

我有编辑Maven插件这种方式。 我真的必须使用src/main/recoures - 它是我们项目的规范。现在

我的问题是,在不同的文件夹中的文件的Unter的主文件夹路径公共/应用是精缩下,但缩小的文件的文件夹处于同一级别比我公共文件夹。

所以,我需要这样的:

<webappTargetDir>**public/app/**${project.build.outputDirectory}</webappTargetDir> 

是否有可能做这样的吗?

<build> 
<plugins> 
    <plugin> 
    <groupId>com.samaxes.maven</groupId> 
    <artifactId>minify-maven-plugin</artifactId> 
    <version>1.7.4</version> 
    <executions> 
     <execution> 
     <id>default-minify</id> 
     <phase>prepare-package</phase><!-- When omitted defaults to 'process-resources' --> 
     <configuration> 
      <charset>UTF-8</charset> 
      <skipMerge>true</skipMerge> 
      <closureCompilationLevel>WHITESPACE_ONLY</closureCompilationLevel> 
      <webappSourceDir>src/main/resources/public/app</webappSourceDir> 
      <webappTargetDir>${project.build.outputDirectory}</webappTargetDir> 
      <cssSourceDir>./</cssSourceDir> 
      <cssSourceIncludes> 
      <cssSourceInclude>**/*.css</cssSourceInclude> 
      </cssSourceIncludes> 
      <jsSourceDir>./</jsSourceDir> 
      <jsSourceIncludes> 
      <jsSourceInclude>**/*.js</jsSourceInclude> 
      </jsSourceIncludes> 
      <jsEngine>CLOSURE</jsEngine> 
     </configuration> 
     <goals> 
      <goal>minify</goal> 
     </goals> 
     </execution> 
    </executions> 
    </plugin> 

随着缩小,Maven的插件配置上面,我得到以下文件夹结构在左侧,但我需要的文件夹结构的分辩侧。有没有可能做到这一点?

Current and needed folder structure

+0

首先,你应该缩小插件的相变化'准备,package'此外,你不应该使用'src/main/resources'并将'webappTargetDir'改为'$ {project.build.outputDirectory}'(target/classes文件夹)......如果使用复制的src/main/resources'文件夹在资源阶段进入'目标/类'这是不是真的想要什么.... – khmarbaise

+0

感谢您的反馈。有没有可能将缩小的文件放在原始文件路径的位置?非常感谢! – quma

+1

如果您尝试使用 $ {project.build.outputDirectory}/public/app',该怎么办? – Tunaki

回答

8

中的文件复制相对从webappSourceDirwebappTargetDir。所以如果你想要结构匹配,你应该使用类似的模式。含义,应该是

<webappSourceDir>src/main/resources</webappSourceDir> 
<webappTargetDir>${project.build.outputDirectory}</webappTargetDir> 

<webappSourceDir>src/main/resources/public/app</webappSourceDir> 
<webappTargetDir>${project.build.outputDirectory}/public/app</webappTargetDir> 

我COMMITED工作实例here

+0

非常感谢!我还有一个问题。如何防止将未缩小的文件打包到罐子中。 – quma

+0

如果它们具有相同的名称,则它们将由缩小器重写。如果它们具有不同的名称,则必须使用通常的资源排除机制排除它们:https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html。 –