Forráskód Böngészése

因playwright加入更新不同环境的打包方式v1

zhiqiang.lv 1 hónapja
szülő
commit
db9e6259fc

+ 35 - 14
pom.xml

@@ -25,6 +25,41 @@
         <version>2.1.9.RELEASE</version>
     </parent>
 
+    <profiles>
+        <profile>
+            <id>dev</id>
+            <activation>
+                <activeByDefault>true</activeByDefault>
+            </activation>
+            <dependencies>
+                <dependency>
+                    <groupId>com.microsoft.playwright</groupId>
+                    <artifactId>playwright</artifactId>
+                    <!--centos7系统需要降级到 v1.37.0 或更低(比如 v1.30.0),centos8可以用1.49.0以上。-->
+                    <!--CentOS 7 默认的 glibc 版本是 2.17,而 Playwright v1.49 自带的 Node.js 需要 glibc 2.27+。-->
+                    <!--运行你的 jar 包,它第一次启动会自动下载 chromium (~130MB) 到 ~/.cache/ms-playwright-->
+                    <version>1.30.0</version>
+                </dependency>
+            </dependencies>
+        </profile>
+        <profile>
+            <id>test</id>
+            <dependencies>
+                <dependency>
+                    <groupId>com.microsoft.playwright</groupId>
+                    <artifactId>playwright</artifactId>
+                    <version>1.30.0</version>
+                    <exclusions>
+                        <exclusion>
+                            <groupId>com.microsoft.playwright</groupId>
+                            <artifactId>driver-bundle</artifactId>
+                        </exclusion>
+                    </exclusions>
+                </dependency>
+            </dependencies>
+        </profile>
+    </profiles>
+
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
@@ -103,20 +138,6 @@
             <artifactId>jsoup</artifactId>
             <version>1.14.3</version>
         </dependency>
-        <dependency>
-            <groupId>com.microsoft.playwright</groupId>
-            <artifactId>playwright</artifactId>
-            <!--centos7系统需要降级到 v1.37.0 或更低(比如 v1.30.0),centos8可以用1.49.0以上。-->
-            <!--CentOS 7 默认的 glibc 版本是 2.17,而 Playwright v1.49 自带的 Node.js 需要 glibc 2.27+。-->
-            <!--运行你的 jar 包,它第一次启动会自动下载 chromium (~130MB) 到 ~/.cache/ms-playwright-->
-            <version>1.30.0</version>
-            <exclusions>
-                <exclusion>
-                    <groupId>com.microsoft.playwright</groupId>
-                    <artifactId>driver-bundle</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 25 - 0
src/main/java/top/lvzhiqiang/service/ScraperService.java

@@ -1,6 +1,9 @@
 package top.lvzhiqiang.service;
 
+import java.io.File;
+
 import org.jsoup.nodes.Document;
+import org.springframework.boot.system.ApplicationHome;
 
 /**
  * 通用接口,以后你要加 JavBus、JavLibrary,直接扩展这个接口即可
@@ -28,4 +31,26 @@ public interface ScraperService {
      * @date: 2026/2/11 15:45
      */
     Document getPageDocumentSecurely(String targetUrl);
+
+
+    /**
+     * 获取项目根目录的通用方法
+     *
+     * @return: java.io.File
+     * @author: lvzhiqiang
+     * @date: 2026/2/12 11:21
+     */
+    default File getAppRootPath() {
+        ApplicationHome home = new ApplicationHome(getClass());
+        File source = home.getSource();
+        
+        // 如果 source 不为空,说明是在 Jar 包里运行,返回 Jar 包同级目录
+        if (source != null) {
+            return source.getParentFile();
+        }
+        
+        // 如果 source 为空,说明是在 IDE 或 测试环境运行
+        // 此时返回项目的根目录 (也就是 System.getProperty("user.dir"))
+        return new File(System.getProperty("user.dir"));
+    }
 }

+ 5 - 3
src/main/java/top/lvzhiqiang/service/impl/AbstractPlaywrightService.java

@@ -19,6 +19,7 @@ import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.annotation.Resource;
 import java.io.BufferedReader;
+import java.io.File;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.InetSocketAddress;
@@ -168,10 +169,10 @@ public abstract class AbstractPlaywrightService implements ScraperService {
 
                 // 截图保留现场,方便排查
                 try {
-                    // 1. 获取 JAR 包所在的绝对路径 (Spring Boot 专属神器)
-                    String jarPath = new ApplicationHome(getClass()).getSource().getParent();
+                    // 1. 获取兼容的根路径
+                    File rootDir = getAppRootPath();
                     // 2. 构造子文件夹路径 (例如: /usr/program/jav/images)
-                    Path dirPath = Paths.get(jarPath, "files/playwright"); // 假设子文件夹叫 images
+                    Path dirPath = Paths.get(rootDir.getAbsolutePath(), "files/playwright");
                     // 3. 关键一步:如果文件夹不存在,必须先创建!
                     if (!Files.exists(dirPath)) Files.createDirectories(dirPath);
                     // 4. 拼接完整的文件路径
@@ -180,6 +181,7 @@ public abstract class AbstractPlaywrightService implements ScraperService {
 
                     page.screenshot(new Page.ScreenshotOptions().setPath(fullPath));
                 } catch (Exception ignored) {
+                    e.printStackTrace();
                 }
             }
             // 失败后刷新页面重试

+ 14 - 11
src/main/java/top/lvzhiqiang/service/impl/JavdbPlaywrightServiceImpl.java

@@ -1,16 +1,18 @@
 package top.lvzhiqiang.service.impl;
 
-import com.microsoft.playwright.Locator;
-import com.microsoft.playwright.Response;
-import lombok.extern.slf4j.Slf4j;
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
-import org.springframework.boot.system.ApplicationHome;
 import org.springframework.stereotype.Service;
 
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
+import com.microsoft.playwright.Locator;
+import com.microsoft.playwright.Response;
+
+import lombok.extern.slf4j.Slf4j;
 
 /**
  * 实现 JavDB 业务类
@@ -107,12 +109,13 @@ public class JavdbPlaywrightServiceImpl extends AbstractPlaywrightService {
             String captchaCode = getCode(imgBytes);
             log.info("验证码识别完成,结果: {}", captchaCode);
             try {
-                // 1. 获取 JAR 包所在的绝对路径 (Spring Boot 专属神器)
-                String jarPath = new ApplicationHome(getClass()).getSource().getParent();
+                // 1. 获取兼容的根路径
+                File rootDir = getAppRootPath();
                 // 2. 构造子文件夹路径 (例如: /usr/program/jav/images)
-                Path dirPath = Paths.get(jarPath, "files/playwright"); // 假设子文件夹叫 images
+                Path dirPath = Paths.get(rootDir.getAbsolutePath(), "files/playwright");
                 // 3. 关键一步:如果文件夹不存在,必须先创建!
-                if (!Files.exists(dirPath)) Files.createDirectories(dirPath);
+                if (!Files.exists(dirPath))
+                    Files.createDirectories(dirPath);
                 // 4. 写入
                 Files.write(dirPath.resolve(captchaCode + ".png"), imgBytes);
             } catch (Exception e) {