Maven build resource file copy failed.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Maven build resource file copy failed.

Ben.Sin
Hi guys,


I come from China. I got an problem "Failed to copy full contents from..."
while using maven to build my project. Because there is an *.xlsx file in
the resources folder. This excel file contains some Chinese characters.

I have tried with add the following configuration in my pom.xls as below.
But it doesn't work.
 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
     <version>3.0.2</version>
                <configuration>
                 <nonFilteredFileExtensions>
                       <nonFilteredFileExtension>
xls</nonFilteredFileExtension>

<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>

                   </nonFilteredFileExtensions>
                </configuration>
            </plugin>
        </plugins>
    </build>

I used "mvn -X clean compile" to build the porject and found the exception
was thrown in the line of following class.

org.apache.maven.shared.utils.io.FileUtils.copyFile(FileUtils.java:839)

FileUitl.copyFile(..) source code is :
private static void doCopyFile( @Nonnull File source, @Nonnull File
destination )
        throws IOException
    {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel input = null;
        FileChannel output = null;
        try
        {
            fis = new FileInputStream( source );
            fos = new FileOutputStream( destination );
            input = fis.getChannel();
            output = fos.getChannel();
            long size = input.size();
            long pos = 0;
            long count;
            while ( pos < size )
            {
                count = size - pos > FILE_COPY_BUFFER_SIZE ?
FILE_COPY_BUFFER_SIZE : size - pos;
                pos += output.transferFrom( input, pos, count );
            }
        }
        finally
        {
            IOUtil.close( output );
            IOUtil.close( fos );
            IOUtil.close( input );
            IOUtil.close( fis );
        }
    }

I tried with junit to call it directory and got the same result.
@Test
public void testFileCopy() throws IOException {
String path = "E:\\test";
String srcFilePath = path + "/my.xlsx";
String destFilePath = path + "/my2.xlsx";
File srcFile = new File(srcFilePath);
File destFile = new File(destFilePath);
FileUtils.copyFile(srcFile, destFile);
assertEquals(srcFile.length(), destFile.length());
}

But it was passed junit test by using the file copy by my own. The source
code is the following.

/**
 * Copy a file to b file.
 * @param src
 * @param dest
 * @return True for successfully. False for failed or empty parameter.
 * @throws RrsException
 */
public static boolean copy(File src, File dest) throws RrsException{
if (src == null || dest == null){
return false;
}
try (
OutputStream os = new FileOutputStream(dest);
){
write(src, os);
} catch (IOException e) {
String msg = MessageFormat.format("Cannot copy file from [{0}] to [{1}]",
src.getAbsolutePath(), dest.getAbsolutePath());
throw new RrsException(msg);
}
return true;
}

/**
 * Write a file with BufferedOutputStream.
 * No close method called in this method for parameter 'os'.
 * The caller should be close it itself.
 * @param file
 * @param os
 * @throws RrsException
 */
public static void write(File file, OutputStream os) throws RrsException{
BufferedOutputStream bos = new BufferedOutputStream(os);

try (
BufferedInputStream bis = new BufferedInputStream(new
FileInputStream(file));
) {
byte[] buffered = new byte[1024 * 256];
int len = 0;
int count = 0;
while((len = bis.read(buffered)) != -1){
bos.write(buffered, 0, len);
if (++count % 10 == 0){
bos.flush();
}
}
bos.flush();
} catch (Exception e) {
throw new RrsException(e.getMessage(), e);
}
}

Could any one have a check that what different between these two way for
copying file?
What configuration can be added to my project to fix the problem?

Thank you very much.


--
THX & B.RGDS
Ben