From 0198ccb063d109956b2f0f9c0ebc404d3f61c356 Mon Sep 17 00:00:00 2001 From: Benny Yen Date: Wed, 26 Jun 2024 18:01:02 +0800 Subject: [PATCH] refactor: move ViteProperties to javite-webmvc --- gradle.properties | 2 +- .../java/com/javite/config/package-info.java | 15 --- .../com/javite/config/VitePropertiesTest.java | 24 ---- .../com/javite/spring/config/ViteConfig.java | 3 +- .../javite/spring}/config/ViteProperties.java | 15 ++- .../com/javite/spring/tags/ViteImport.java | 2 +- .../src/main/resources/META-INF/vite.tld | 8 +- .../com/javite/spring/config/ViteConfig.java | 3 +- .../javite/spring/config/ViteProperties.java | 124 ++++++++++++++++++ .../com/javite/spring/tags/ViteImport.java | 2 +- .../src/main/resources/META-INF/vite.tld | 2 +- .../javite/spring/config/ViteConfigTest.java | 5 +- .../javite/spring/tags/ViteImportTest.java | 2 +- 13 files changed, 149 insertions(+), 58 deletions(-) delete mode 100644 javite-core/src/main/java/com/javite/config/package-info.java delete mode 100644 javite-core/src/test/java/com/javite/config/VitePropertiesTest.java rename {javite-core/src/main/java/com/javite => javite-webmvc-jre8/src/main/java/com/javite/spring}/config/ViteProperties.java (89%) create mode 100644 javite-webmvc/src/main/java/com/javite/spring/config/ViteProperties.java diff --git a/gradle.properties b/gradle.properties index 33aa3f7..dfe35db 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=0.1.2 +version=0.1.3-alpha diff --git a/javite-core/src/main/java/com/javite/config/package-info.java b/javite-core/src/main/java/com/javite/config/package-info.java deleted file mode 100644 index 9480546..0000000 --- a/javite-core/src/main/java/com/javite/config/package-info.java +++ /dev/null @@ -1,15 +0,0 @@ -/** - * This package contains core configuration classes for the JaVite library. - * - *

Currently, this package includes the {@link com.javite.config.ViteProperties} class, - * which provides properties for configuring Vite in various Java web applications. - * These properties include settings for the Vite manifest file, local development server URL, - * resource paths, and debug mode.

- * - *

The {@link com.javite.config.ViteProperties} class is designed to be used by various - * integration modules, such as those for Spring MVC, to provide consistent configuration - * and ease of setup for applications using Vite.

- * - * @see com.javite.config.ViteProperties - */ -package com.javite.config; diff --git a/javite-core/src/test/java/com/javite/config/VitePropertiesTest.java b/javite-core/src/test/java/com/javite/config/VitePropertiesTest.java deleted file mode 100644 index 0bd5ae9..0000000 --- a/javite-core/src/test/java/com/javite/config/VitePropertiesTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.javite.config; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; - -public class VitePropertiesTest { - - @Test - void testGettersAndSetters() { - ViteProperties properties = new ViteProperties(); - - properties.setDebug(false); - properties.setManifestPath("/.vite/manifest.json"); - properties.setLocalServerUrl("http://localhost:5173"); - properties.setResourcePath("/resources"); - - assertThat(properties.isDebug()).isFalse(); - assertThat(properties.getManifestPath()).isEqualTo("/.vite/manifest.json"); - assertThat(properties.getLocalServerUrl()).isEqualTo("http://localhost:5173"); - assertThat(properties.getResourcePath()).isEqualTo("/resources"); - } - -} diff --git a/javite-webmvc-jre8/src/main/java/com/javite/spring/config/ViteConfig.java b/javite-webmvc-jre8/src/main/java/com/javite/spring/config/ViteConfig.java index 3ab71a0..b703872 100644 --- a/javite-webmvc-jre8/src/main/java/com/javite/spring/config/ViteConfig.java +++ b/javite-webmvc-jre8/src/main/java/com/javite/spring/config/ViteConfig.java @@ -1,8 +1,8 @@ package com.javite.spring.config; -import com.javite.config.ViteProperties; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** @@ -31,6 +31,7 @@ * @see com.javite.spring.annotation.EnableVite */ @Configuration +@ComponentScan(basePackages = "com.javite.spring.config") public class ViteConfig { @Value("${vite.debug:true}") diff --git a/javite-core/src/main/java/com/javite/config/ViteProperties.java b/javite-webmvc-jre8/src/main/java/com/javite/spring/config/ViteProperties.java similarity index 89% rename from javite-core/src/main/java/com/javite/config/ViteProperties.java rename to javite-webmvc-jre8/src/main/java/com/javite/spring/config/ViteProperties.java index 3e338f4..f898fb1 100644 --- a/javite-core/src/main/java/com/javite/config/ViteProperties.java +++ b/javite-webmvc-jre8/src/main/java/com/javite/spring/config/ViteProperties.java @@ -1,15 +1,17 @@ -package com.javite.config; +package com.javite.spring.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; /** * Configuration properties for integrating Vite with Java web applications. * *

This class provides properties that can be used to configure the Vite integration, - * including settings for the Vite manifest file, local development server URL, resource paths, - * and debug mode.

+ * including settings for the Vite manifest file, local development server URL, resource paths, and debug mode.

* *

These properties can be configured in the application's configuration file (e.g., `application.properties` - * or `application.yml` for Spring applications) and are typically used by integration modules to - * facilitate the setup and use of Vite in Java web applications.

+ * or `application.yml` for Spring applications) and are typically used by integration modules to facilitate the setup and use of Vite in Java web + * applications.

* *

Usage example:

*
@@ -19,6 +21,8 @@
  * vite.resourcePath=/resources
  * 
*/ +@Component +@ConfigurationProperties(prefix = "vite") public class ViteProperties { /** @@ -116,4 +120,5 @@ public String getResourcePath() { public void setResourcePath(String resourcePath) { this.resourcePath = resourcePath; } + } diff --git a/javite-webmvc-jre8/src/main/java/com/javite/spring/tags/ViteImport.java b/javite-webmvc-jre8/src/main/java/com/javite/spring/tags/ViteImport.java index 0a257d8..0fe3abb 100644 --- a/javite-webmvc-jre8/src/main/java/com/javite/spring/tags/ViteImport.java +++ b/javite-webmvc-jre8/src/main/java/com/javite/spring/tags/ViteImport.java @@ -1,7 +1,7 @@ package com.javite.spring.tags; import com.fasterxml.jackson.databind.JsonNode; -import com.javite.config.ViteProperties; +import com.javite.spring.config.ViteProperties; import com.javite.util.HtmlUtils; import com.javite.util.ManifestUtils; import java.io.IOException; diff --git a/javite-webmvc-jre8/src/main/resources/META-INF/vite.tld b/javite-webmvc-jre8/src/main/resources/META-INF/vite.tld index 96d6063..733f819 100644 --- a/javite-webmvc-jre8/src/main/resources/META-INF/vite.tld +++ b/javite-webmvc-jre8/src/main/resources/META-INF/vite.tld @@ -7,6 +7,10 @@ Vite JSP Tag Library vite + Import a Vite asset. + import + com.javite.spring.tags.ViteImport + empty The entry point of the Vite asset to be imported. entry @@ -37,10 +41,6 @@ false true - empty - Import a Vite asset. - import - com.javite.spring.tags.ViteImport 1.0 diff --git a/javite-webmvc/src/main/java/com/javite/spring/config/ViteConfig.java b/javite-webmvc/src/main/java/com/javite/spring/config/ViteConfig.java index 8f301af..23769f0 100644 --- a/javite-webmvc/src/main/java/com/javite/spring/config/ViteConfig.java +++ b/javite-webmvc/src/main/java/com/javite/spring/config/ViteConfig.java @@ -1,9 +1,9 @@ package com.javite.spring.config; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import com.javite.config.ViteProperties; /** * Configuration class for integrating Vite with Spring MVC applications. @@ -12,6 +12,7 @@ * {@code application.properties} file.

*/ @Configuration +@EnableConfigurationProperties(ViteProperties.class) public class ViteConfig { @Value("${vite.debug:true}") diff --git a/javite-webmvc/src/main/java/com/javite/spring/config/ViteProperties.java b/javite-webmvc/src/main/java/com/javite/spring/config/ViteProperties.java new file mode 100644 index 0000000..f898fb1 --- /dev/null +++ b/javite-webmvc/src/main/java/com/javite/spring/config/ViteProperties.java @@ -0,0 +1,124 @@ +package com.javite.spring.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * Configuration properties for integrating Vite with Java web applications. + * + *

This class provides properties that can be used to configure the Vite integration, + * including settings for the Vite manifest file, local development server URL, resource paths, and debug mode.

+ * + *

These properties can be configured in the application's configuration file (e.g., `application.properties` + * or `application.yml` for Spring applications) and are typically used by integration modules to facilitate the setup and use of Vite in Java web + * applications.

+ * + *

Usage example:

+ *
+ * vite.debug=true
+ * vite.manifestPath=/WEB-INF/dist/.vite/manifest.json
+ * vite.localServerUrl=http://localhost:5173
+ * vite.resourcePath=/resources
+ * 
+ */ +@Component +@ConfigurationProperties(prefix = "vite") +public class ViteProperties { + + /** + * Whether to enable debug mode in Vite. + *

Default is {@code true}.

+ */ + private boolean debug = true; + + /** + * The path of the manifest file generated by Vite. + *

Default is {@code /WEB-INF/dist/.vite/manifest.json}.

+ */ + private String manifestPath = "/WEB-INF/dist/.vite/manifest.json"; + + /** + * The local server URL for Vite development. + *

Default is {@code http://localhost:5173}.

+ */ + private String localServerUrl = "http://localhost:5173"; + + /** + * The path to the resources. + *

Default is {@code /resources}.

+ */ + private String resourcePath = "/resources"; + + /** + * Returns whether debug mode is enabled. + * + * @return {@code true} if debug mode is enabled, {@code false} otherwise + */ + public boolean isDebug() { + return debug; + } + + /** + * Sets whether debug mode is enabled. + * + * @param debug {@code true} to enable debug mode, {@code false} to disable it + */ + public void setDebug(boolean debug) { + this.debug = debug; + } + + /** + * Returns the path of the manifest file generated by Vite. + * + * @return the manifest file path + */ + public String getManifestPath() { + return manifestPath; + } + + /** + * Sets the path of the manifest file generated by Vite. + * + * @param manifestPath the manifest file path + */ + public void setManifestPath(String manifestPath) { + this.manifestPath = manifestPath; + } + + /** + * Returns the local server URL for Vite development. + * + * @return the local server URL + */ + public String getLocalServerUrl() { + return localServerUrl; + } + + /** + * Sets the local server URL for Vite development. + * + * @param localServerUrl the local server URL + */ + public void setLocalServerUrl(String localServerUrl) { + this.localServerUrl = localServerUrl; + } + + /** + * Returns the path to the resources. + * + * @return the resource path + */ + public String getResourcePath() { + return resourcePath; + } + + /** + * Sets the path to the resources. + * + * @param resourcePath the resource path + */ + public void setResourcePath(String resourcePath) { + this.resourcePath = resourcePath; + } + +} diff --git a/javite-webmvc/src/main/java/com/javite/spring/tags/ViteImport.java b/javite-webmvc/src/main/java/com/javite/spring/tags/ViteImport.java index e7683be..460dec6 100644 --- a/javite-webmvc/src/main/java/com/javite/spring/tags/ViteImport.java +++ b/javite-webmvc/src/main/java/com/javite/spring/tags/ViteImport.java @@ -1,7 +1,7 @@ package com.javite.spring.tags; import com.fasterxml.jackson.databind.JsonNode; -import com.javite.config.ViteProperties; +import com.javite.spring.config.ViteProperties; import com.javite.util.HtmlUtils; import com.javite.util.ManifestUtils; import jakarta.servlet.ServletContext; diff --git a/javite-webmvc/src/main/resources/META-INF/vite.tld b/javite-webmvc/src/main/resources/META-INF/vite.tld index 941c56e..f134019 100644 --- a/javite-webmvc/src/main/resources/META-INF/vite.tld +++ b/javite-webmvc/src/main/resources/META-INF/vite.tld @@ -7,7 +7,7 @@ Vite JSP Tag Library 1.0 vite - https://javite.com + https://javite.com/tags Import a Vite asset. diff --git a/javite-webmvc/src/test/java/com/javite/spring/config/ViteConfigTest.java b/javite-webmvc/src/test/java/com/javite/spring/config/ViteConfigTest.java index dd8597f..5c8372a 100644 --- a/javite-webmvc/src/test/java/com/javite/spring/config/ViteConfigTest.java +++ b/javite-webmvc/src/test/java/com/javite/spring/config/ViteConfigTest.java @@ -1,13 +1,12 @@ package com.javite.spring.config; -import com.javite.config.ViteProperties; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; -import static org.assertj.core.api.Assertions.assertThat; - @SpringBootTest(classes = ViteConfigTest.TestConfig.class) public class ViteConfigTest { diff --git a/javite-webmvc/src/test/java/com/javite/spring/tags/ViteImportTest.java b/javite-webmvc/src/test/java/com/javite/spring/tags/ViteImportTest.java index b465a6d..d1b3e30 100644 --- a/javite-webmvc/src/test/java/com/javite/spring/tags/ViteImportTest.java +++ b/javite-webmvc/src/test/java/com/javite/spring/tags/ViteImportTest.java @@ -5,7 +5,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import com.javite.config.ViteProperties; +import com.javite.spring.config.ViteProperties; import jakarta.servlet.ServletContext; import jakarta.servlet.jsp.JspWriter; import jakarta.servlet.jsp.PageContext;