fix(项目设置): 修改代码片段执行

This commit is contained in:
shiziyuan9527 2022-10-21 11:50:51 +08:00 committed by lyh
parent 8eab9189ff
commit 7183e066de
2 changed files with 72 additions and 0 deletions

View File

@ -117,6 +117,53 @@
<release>${java.version}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>generate-resources</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_functions</artifactId>
<version>${jmeter.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory>
<destFileName>ApacheJMeter_functions.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>io.metersphere</groupId>
<artifactId>metersphere-jmeter-functions</artifactId>
<version>${metersphere-jmeter-functions.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory>
<destFileName>metersphere-jmeter-functions.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>${jython.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory>
<destFileName>jython-standalone.jar</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</plugin>
</plugins>
</build>

View File

@ -2,6 +2,8 @@ package io.metersphere.listener;
import io.metersphere.commons.utils.LogUtil;
import org.apache.jmeter.util.JMeterUtils;
import org.python.core.Options;
import org.python.util.PythonInterpreter;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.i18n.LocaleContextHolder;
@ -14,8 +16,13 @@ public class ProjectAppStartListener implements ApplicationListener<ApplicationR
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
LogUtil.info("================= PROJECT 应用启动 =================");
this.initJmeterHome();
LogUtil.info("导入内置python包处理");
this.initPythonEnv();
}
private void initJmeterHome() {
String JMETER_HOME = Objects.requireNonNull(getClass().getResource("/")).getPath() + "jmeter";
String JMETER_PROPERTIES = JMETER_HOME + "/bin/jmeter.properties";
@ -23,4 +30,22 @@ public class ProjectAppStartListener implements ApplicationListener<ApplicationR
JMeterUtils.setJMeterHome(JMETER_HOME);
JMeterUtils.setLocale(LocaleContextHolder.getLocale());
}
/**
* 解决接口测试-无法导入内置python包
*/
private void initPythonEnv() {
//解决无法加载 PyScriptEngineFactory
try {
Options.importSite = false;
PythonInterpreter interpreter = new PythonInterpreter();
StringBuffer pathBuffer = new StringBuffer(getClass().getResource("/").getPath() + "jmeter");
pathBuffer.append("/lib/ext/jython-standalone.jar/Lib");
interpreter.exec("import sys");
interpreter.exec("sys.path.append(\"" + pathBuffer.toString() + "\")");
LogUtil.info("sys.path: ", pathBuffer.toString());
} catch (Exception e) {
LogUtil.error("导入内置python包处理异常 ", e);
}
}
}