Maven项目在生产环境中寻找一个不存在的路径。

huangapple go评论69阅读模式
英文:

Maven Project looking for a non existing path at prod

问题

我在我的软件中使用了一个配置文件,我将文件放在了正确的根目录(非本地)src/main/java/pdf/factory/fop.xconf。在编译代码后,在本地它会在以下路径寻找该文件:fopBaseUri=C:\Users\user\Documents\software\local\target\data\bin\content\sftw.web-0.0.0-SNAPSHOT.war\WEB-INF\classes\pdf\fopfactorybasedir\fop.xconf

我没有那个路径,只有C:\Users\user\Documents\software\local\target\data\bin,所以我创建了路径的其余部分,它按预期工作了。

然而,当我部署时,它不起作用,可能是因为在编译后该路径不存在,并且在运行mvn install时会全部清除。

如何强制它查找该路径,或者强制使用我想要的文件(非本地位于src/...)?

以下是我的代码:

@ApplicationScoped
public class FopFactoryService {
    private static final String ROOT_PATH = "/pdf/fopfactorybasedir/fop.xconf";
    private final FopFactory fopFactory;
    private final FOUserAgent foUserAgent;
    private final TransformerFactory transformerFactory;

    public FopFactoryService() throws URISyntaxException, IOException, SAXException {
        final URI uri = FopFactoryService.class.getResource(ROOT_PATH).toURI();
        final File f = new File(uri.getPath());
        LOG.info("fopBaseUri={}", f);
        fopFactory = FopFactory.newInstance(f);
        foUserAgent = fopFactory.newFOUserAgent();
        transformerFactory = TransformerFactory.newInstance();
    }
}

我漏掉了什么?

英文:

I am using a configuration file into my software, i have the file in the correct root(non local) src/main/java/pdf/factory/fop.xconf. After compile the code, locally it looked for the file into fopBaseUri=C:\Users\user\Documents\software\local\target\data\bin\content\sftw.web-0.0.0-SNAPSHOT.war\WEB-INF\classes\pdf\fopfactorybasedir\fop.xconf

I didn't have that path, just had C:\Users\user\Documents\software\local\target\data\bin
so i've created the rest of the path, and it worked like it should..

however, when i deployed, it didn't work, maybe becuase the path don't exist after compile, and it wipes it all when mvn install.

how can i force it to look into the path, or force the file to be the one i use ( non local into src/... )

heres my code:

@ApplicationScoped
public class FopFactoryService {
    private static final String ROOT_PATH = "/pdf/fopfactorybasedir/fop.xconf";
    private final FopFactory fopFactory;
    private final FOUserAgent foUserAgent;
    private final TransformerFactory transformerFactory;

    public FopFactoryService() throws URISyntaxException, IOException, SAXException {
        final URI uri = FopFactoryService.class.getResource(ROOT_PATH).toURI();
        final File f = new File(uri.getPath());
        LOG.info("fopBaseUri={}", f);
        fopFactory = FopFactory.newInstance(f);
        foUserAgent = fopFactory.newFOUserAgent();
        transformerFactory = TransformerFactory.newInstance();
    }

what am i missing?

答案1

得分: 0

文件在maven构建后现在可以在服务器上找到。

vfs路径是内部的,是Jboss为自己创建的虚拟文件系统,但仅在jboss使用的内存内部。new File()会查找真实的硬盘位置。因为这只是在虚拟环境中,所以并不存在这样的路径。

自从Jboss版本5以来,这是一个常见的问题,所以解决方案可以通过这个问题的回答来找到:https://stackoverflow.com/questions/22567174/not-getting-absolute-file-path-from-resources

如果你遇到java.lang.ClassCastException: java.io.FileInputStream cannot be cast to org.jboss.vfs.VirtualFile,只需尝试在不进行转换的情况下使用。

希望能对某人有所帮助。

英文:

The file can be now located at the server after maven build.

The vfs path is internal, virtual file system Jboss creates for itself, but only inside memory used by jboss. The new File() looks on the real harddrive location. There is no such path, because it is only virtual there.
That's a common problem since Jboss version 5, so the solution pass through this question answered here: https://stackoverflow.com/questions/22567174/not-getting-absolute-file-path-from-resources
if you get java.lang.ClassCastException: java.io.FileInputStream cannot be cast to org.jboss.vfs.VirtualFile just try that without cast.
hope it help somebody.

huangapple
  • 本文由 发表于 2020年5月5日 19:58:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61612596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定