views:

42

answers:

2

I wasn't sure what the best title is so if anyone has a better suggestion, fire away.

I'm not sure what information I should be providing, so I'll tell you what is happening.

I have some unit tests that pass fine in Eclipse. In these tests, there's some XML that's being validated against a custom built DTD (a slightly modified Apelon DTS DTSConcept if anyone is familiar). In eclipse, when I run these tests, I can see in my target path for the project that the dtds show up.

So, dir1/dir2/dtds/myDtd.dtd exists in the target path of the project.

However, if I run the unit tests on the command line with maven (mvn clean test), these tests failed because of a MalformedURLException. I was able to get rid of DTD validation and the tests passed, so I knew it had something to do with that. After many things tried, for whatever reason I looked in the target path of the project. Now, dir1/dir2/dtds/myDtd.dtd did NOT exist. That seems to be why I'm getting the exception -- the file doesn't exist.

I realize this may be too vague, but is there anything that you can think of why I might be having these different results running in Eclipse versus using the Maven command line?

I will provide more information as requested, but I wasn't sure what exactly to include.

Thanks for any assistance.

EDIT: Okay, it seems that the problem is that maven simply doesn't copy the DTDs over to the target directory when building. I may google/ask a separate question for this, but how would I ensure Maven copies those files correctly?

A: 

Well, I figured it out :) In case anyone runs into a similar issue and can't quite figure out the plugin initially (like me), here's what worked for me:

<plugin>
            <!-- Necessary for maven to copy the DTDs to the correct directory. -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <!-- here the phase you need -->
                    <phase>compile</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/classes/com/work/hin/dts/dtd/query</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/java/com/work/hin/dts/dtd/query</directory>
                                <includes>
                                    <include>**/*.dtd</include>
                                </includes>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
AHungerArtist
+2  A: 

Hi,

Considering that the file is a DTD (not a Java ressource) I think it would be better to put it in the resources/META-INF folder of a default maven project.

By default, maven copies all files under resources to the target destination during the process-resources phase, so you could get rid of extra plugin configuration.

It would also require modifying your XML file to point to the new location.

Here's what it would look like in typical maven project strucutre:

my-app
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |   `-- com
    |   |       `-- mycompany
    |   |           `-- app
    |   |               `-- App.java
    |   `-- resources
    |       `-- META-INF
    |           `-- application.properties
    [            -- myDtd.dtd
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java
Guillermo Moscoso
Thank you. I am aware of that (and that's where we put other resources we use) but I'm using a third party library that requires them to be in a certain place to be used. Otherwise, you're right, it would be much easier to put them in there.
AHungerArtist
Actually this is applicable as long as I extend out my resources folder to include the directory (so main/resources/dir1/dir2/blah). So I'll mark it correct with that caveat.
AHungerArtist
I see, I didn't realize that. By the way do you use the m2eclipse plugin (eclipse plugin, not maven plugin)? If you do the eclipse build should behave the same way the maven build does. This should avoid the original problem of having different results when using one or the other.
Guillermo Moscoso
I haven't been using that but it's definitely a good suggestion. Thanks.
AHungerArtist