maven依赖中的文件访问

时间:2017-11-14 09:35:29

标签: java maven

我有一个由3个子项目组成的maven项目:公共,服务器和客户端,其中客户端和服务器依赖于公共但不相互了解。

在公共场所,我必须创建和读/写文件,但我最终得到一个AccessControlException:当我尝试对文件做某事时,访问被拒绝异常。

例外情况如下:

java.security.AccessControlException: access denied ("java.io.FilePermission" "test.txt" "write")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.security.AccessController.checkPermission(AccessController.java:884)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at java.lang.SecurityManager.checkWrite(SecurityManager.java:979)
at java.io.File.createNewFile(File.java:1008)

即使我在commons中执行以下代码,我也会收到错误:

File file = new File("test.txt");
file.createNewFile();    //write exception
FileInputStream fis = new FileInputStream(file); //if file would exist it would throw read exception 

如果我在客户端或服务器上执行此代码,它运行得很好。

服务器的pomfile如下所示:

<?xml version="1.0" encoding="UTF-8"?>
     <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>

<artifactId>server</artifactId>
<groupId>my.project</groupId>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>



<dependencies>
    ...
    <dependency>
        <groupId>my.project.commons</groupId>
        <artifactId>commons</artifactId>
        <version>0.1 Alpha</version>
    </dependency>
    <dependency>
        <groupId>my.project.commons</groupId>
        <artifactId>commons</artifactId>
        <version>0.1 Alpha</version>
        <classifier>tests</classifier>
        <scope>test</scope>
    </dependency>
    ...
</dependencies>
</project>

公共的pomfile如下所示:

 <?xml version="1.0" encoding="UTF-8"?>
<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>

<artifactId>commons</artifactId>
<groupId>my.project.commons</groupId>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
</dependencies>
</project>

您是否有任何错误或错误的想法?

0 个答案:

没有答案