2024年1月5日 星期五

Maven 自動 Compile SASS/SCSS (使用 nl.geodienstencentrum.maven plugin)

SASS / SCSS 太強了
而且他還沒有使出全力的樣子
對方就算沒有 @mixin 或是 @include 也會贏
我甚至覺得有點對不起他……

不得不說,SCSS 真的太好用了。

就算沒有 @mixin 或是 @include,光是巢狀架構的處理就夠好用了。

雖然現在 CSS 也開始要支援巢狀寫法(Using CSS nesting - CSS: Cascading Style Sheets | MDN (mozilla.org)),但是現在 CSS 的複雜度都已經媲美 JS 了,還要追加巢狀架構,我真心覺得這些 web 委員會是不是沒事找事做啊。


不提這個,這邊只是要記錄一下,如何在 Java Maven 讓其在 compile / package 時,自動編譯 SASS

pom.xml 的設定

使用的 plugin 是 nl.geodienstencentrum.maven 的 Sass Compiler Plugin

整個專案的 pom.xml 如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<!-- 中略 -->
	<build> 
		<plugins> 
			<plugin>
				<groupId>nl.geodienstencentrum.maven</groupId>
				<artifactId>sass-maven-plugin</artifactId>
				<!-- 版本我這邊是使用最新的 3.7.2,如果是使用 Java 1.8 就需要降版。目前測試的結果是可以用 3.6.0 -->
				<version>3.7.2</version> 
				<executions>
				<execution>
					<id>build</id>
					<phase>generate-resources</phase>
					<goals>
						<goal>update-stylesheets</goal>
					</goals>
				</execution>
				</executions>
				<configuration>
					<!-- SASS Folder 設定,這邊是將 ${project.basedir}/src/main/resources/sass Compile 至 ${project.build.directory}/classes/resources/styles -->
					<sassSourceDirectory>
						${project.basedir}/src/main/resources/sass
					</sassSourceDirectory>
					<destination> 
						${project.build.directory}/classes/resources/styles
					</destination>
				</configuration>
			</plugin>
		</plugins>
	</build> 
</project>
較新版本的 Java

如果是使用 Java 1.9 以上進行 compile ,會跳出以下的警告訊息

Pass '--add-opens java.base/sun.nio.ch=org.jruby.dist' or '=org.jruby.core' to enable.
java.lang.IllegalCallerException: sun.nio.ch is not open to unnamed module @7fd4e815
        at java.base/java.lang.Module.addOpens(Module.java:892)
        at com.headius.backport9.modules.impl.Module9.addOpens(Module9.java:28)
        at com.headius.backport9.modules.Modules.addOpens(Modules.java:22)
        at org.jruby.util.io.FilenoUtil$ReflectiveAccess.<clinit>(FilenoUtil.java:140)
        at org.jruby.util.io.FilenoUtil.getFilenoUsingReflection(FilenoUtil.java:111)
        at org.jruby.util.io.FilenoUtil.filenoFrom(FilenoUtil.java:107)
        at org.jruby.util.io.ChannelFD.initFileno(ChannelFD.java:42)
        at org.jruby.util.io.ChannelFD.<init>(ChannelFD.java:32)
        at org.jruby.util.io.OpenFile.setChannel(OpenFile.java:196)
        at org.jruby.RubyIO.prepIO(RubyIO.java:261)
        at org.jruby.RubyIO.prepStdio(RubyIO.java:190)
        at org.jruby.RubyGlobal.initSTDIO(RubyGlobal.java:297)
        at org.jruby.RubyGlobal.createGlobals(RubyGlobal.java:211)
        at org.jruby.Ruby.init(Ruby.java:1260)
        at org.jruby.Ruby.newInstance(Ruby.java:370)
        at org.jruby.embed.internal.LocalContext.getRuntime(LocalContext.java:117)
        at org.jruby.embed.internal.SingleThreadLocalContextProvider.getRuntime(SingleThreadLocalContextProvider.java:62)
        at org.jruby.embed.internal.BiVariableMap.getRuntime(BiVariableMap.java:109)
        at org.jruby.embed.internal.BiVariableMap.getTopSelf(BiVariableMap.java:241)
        at org.jruby.embed.internal.BiVariableMap.getReceiverObject(BiVariableMap.java:237)
        at org.jruby.embed.internal.BiVariableMap.put(BiVariableMap.java:332)

出現這個狀況,最簡單就是直接在 mvn 指令中加入要求參數,例如

mvn clean package --add-opens java.base/sun.nio.ch=org.jruby.dist

不過我自己並沒特別處理。如果要在 maven pom.xml 中加參數迴避,應該能參考 stackoverflow: 《How can I specify --add-opens from a project level and make sure it is taken into account whatever the means to run my app?》 這篇文章

參考資料

Cox, G., & Cox, G. (2017, April 25). Building your Front End with Maven: Simple Resources. SitePoint. https://www.sitepoint.com/building-front-end-maven-simple-resources/

沒有留言:

張貼留言