gpt4 book ai didi

java - 如何正确地将资源添加到我的 java 项目中,以便它们可供构建的实例使用?

转载 作者:行者123 更新时间:2023-12-02 04:17:31 24 4
gpt4 key购买 nike

我似乎无法构建一个与我的项目中的资源一起使用的“解决方案”。当我在构建期间运行测试时,它自然可以访问资源,但构建后测试找不到该文件。如何调整我的代码以便它在这两种情况下都能工作,或者调整我的项目。如果时间允许的话,除了我使用 maven-shade 插件的特殊情况之外,我还有兴趣了解与此相关的 Java 项目设置的基础知识。

我尝试了各种路径变化,但没有成功

我尝试过 getClass().ClassLoader 这个那个,但没有成功。

我的pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>company.myproj</groupId>
<artifactId>myproj</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>true</skipTests>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0-beta3</version>
<!-- <scope>provided</scope> -->
</dependency>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
<!-- <scope>provided</scope> -->
</dependency>

</dependencies>


<build>

<!-- Source directory configuration -->
<sourceDirectory>src</sourceDirectory>

<resources>
<resource>
<directory>src/resources</directory>
</resource>
</resources>

<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>


<!-- // Following plugin executes the testng tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<!-- // Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testtest.xml</suiteXmlFile>
</suiteXmlFiles>
<skipTests>${skipTests}</skipTests>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes><exclude>META-INF/versions/**</exclude></excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.testng.TestNG</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>

</project>

我的java

package myproj;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.testng.annotations.Test;

public class TestTest{

@Test(groups = { "basic1" })
public void Test05BasicPASS() throws URISyntaxException, IOException {


Path filePath = Paths.get(getClass().getResource("/images/hydrant.jpg").toURI());


System.out.println("My Path is: "+filePath.toString());


System.out.println("This is test 5, Basic Pass");
}

}

我的testNg TestTest.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SuiteB1" parallel="false">
<test name="TestTest">

<groups>
<run>
<include name="basic1"/>
</run>
</groups>

<classes>
<class name="myproj.TestTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

我的目录结构:

src
-myproj
TestTest.java
-resources
-images
hydrant.jpg

TestTest.xml
pom.xml

当我运行时:mvn package -DskipTests=false测试运行并可以访问图像文件。

构建后,当我在命令行上运行时(构建到“目标”目录:java -jar C:\Dev\testproj\target\myproj-0.0.1.jar TestTest.xml

我在访问文件的行上得到了 FileSystemNotFoundException 测试结果。

最佳答案

最后,我决定 Java“规定”的约定(在特定位置需要目录和在一种情况下工作的方法而不是另一种情况)对于我正在实现的简单测试应用程序来说太复杂了。

我发现以下内容以简单的方式支持我所需要的:

对于初学者来说,我使用的图像用于测试比较,因此由于可能需要即时交换这些图像以进行调试,因此将目录置于应用程序外部会有所帮助。如果有人分享如何更改此问题的标题,我很乐意这样做。

所以,考虑到这一点。现在,我的项目根目录中有一个“images”目录,并且该目录将部署在应用程序附近,从而产生以下基本产品:

-myTestApp
myTestApp.jar
-images
baseImage.jpg
testImage.jpg

我用来从测试方法访问文件的代码如下,适用于构建测试并作为 jar 应用程序运行:

Path screenshotsPath = Paths.get(System.getProperty("user.dir"), "images");

Path baseFile = Paths.get(screenshotsPath.toString(), "baseImage.jpg" );
Path testFile = Paths.get(screenshotsPath.toString(), "testImage.jpg" );

double imageDiff = calcImageDiff(baseFile, testFile);

在 calcImageDiff 中,我仅使用以下内容,其中 file1 和 file2 是路径输入:

img1 = ImageIO.read(file1.toFile());
img2 = ImageIO.read(file2.toFile());

关于java - 如何正确地将资源添加到我的 java 项目中,以便它们可供构建的实例使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56655060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com