17

I'm working on a very simple blog engine in Java in order to learn multiple technologies.

Tech: Spring IoC, Hibernate, jUnit, GWT, and Maven.

I created two Maven projects: a core project and a GWT project (which has a reference on the core one)

Code is accessible at https://github.com/LaurentT/BlogEngineCore

My goal is the following: I want to include Java sources and XML since my GWT project is going to need the Java sources to compile it into JavaScript.

I tried to use the following code in the <build> element:

     <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.*xml</include>
                <include>**/*.*properties</include>
            </includes>
        </resource>
    </resources>

My jUnit tests used to pass and finish before that add but now they are not even finishing they are hanging...

I have no clue what's going on, so I want to know if there are other ways to include Java sources or if I'm just doing it wrong.

Any clue?

mkobit
  • 43,979
  • 12
  • 156
  • 150
Laurent T
  • 1,070
  • 4
  • 13
  • 25
  • why do you think "junit tests are hanging" has anything to do with including java sources? – milan Jan 04 '12 at 17:51
  • It's just that if I remove those lines my tests are finishing and passing. I don't know why but that's what it is doing :( – Laurent T Jan 05 '12 at 08:34
  • Possible duplicate of [Maven create jar file with both .class and .java files](https://stackoverflow.com/questions/4264359/maven-create-jar-file-with-both-class-and-java-files) – Vadzim Apr 05 '18 at 09:39

4 Answers4

25

The cleaner maven way would be to attach a separate source jar.

There are standard ways to generate it in your build using the maven source plugin:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-source-plugin</artifactId>
  <version>2.2.1</version>
  <executions>
    <execution>
      <id>attach-sources</id>
      <phase>verify</phase>
      <goals>
        <goal>jar-no-fork</goal>
      </goals>
    </execution>
  </executions>
</plugin>

And your GWT project can now reference your core project sources in addition to your core project jar:

<dependency>
  <groupId>your.project</groupId>
  <artifactId>core</artifactId>
  <version>the.same.version</version>
  <classifier>sources</classifier>
  <scope>provided</scope><!-- use for compilation only -->
</dependency>
mkobit
  • 43,979
  • 12
  • 156
  • 150
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
12

try including the ${basedir} in front of the directory path.

<resource>
    <directory>${basedir}/src/main/java</directory>
</resource>
mkobit
  • 43,979
  • 12
  • 156
  • 150
Mike Pone
  • 18,705
  • 13
  • 53
  • 68
  • Sean's solution was good if I wanted to have two separate JARs but here since my GWT project is loading one jar (the core project) I really need one JAR to be loaded in the classpath where I can find my sources. This worked like a charm! thanks a lot Mike – Laurent T Jan 05 '12 at 10:11
  • This will include all files in your source dir. While you probably want to include only `*.gwt.xml` and sources from `client` and `shared` dirs. – Vlastimil Ovčáčík Feb 04 '16 at 13:07
3

You can find how to setup maven for multimodule project here.

Where's the *gwt.xml file?

If the tag shown is part of the core project's pom.xml file, then you should also add <include>**/*.gwt.xml</include>:

<resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.java</include>
            <include>**/*.gwt.xml</include>
        </includes>
</resource>
mkobit
  • 43,979
  • 12
  • 156
  • 150
milan
  • 11,872
  • 3
  • 42
  • 49
  • This worked for me, however be careful, this will cause everything in src/main/resources to NOT be included in the jar. fix that simply by copying this and adding another resources for src/main/resources, including the appropriate extensions / wildcards – Ali Dec 21 '15 at 02:22
  • This example includes only the files required for gwt module, which is better than include blindly everything in src dir. Thanks! – Vlastimil Ovčáčík Feb 04 '16 at 12:57
  • @ClickUpvote you're right, the example was edited to make it more portable. It is drop-in now imho. – Vlastimil Ovčáčík Feb 04 '16 at 12:58
0

In contrast to the other answers, my approach copies all *.gwt.xml module descriptors and only the java source files that are actually specified in the module descriptors. You also don't need to repeat yourself in POM and module descriptors.

Update your pom with:

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>gwt-maven-plugin</artifactId>
            <version>2.7.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

and your *.gwt.xml

<?xml version="1.0" encoding="UTF-8"?>
<module>

    <!-- Specify the paths for translatable code -->
    <source path='shared' />

</module>

org:codehaus.mojo:gwt-maven-plugin docs

gwt:resources
Description: Copy GWT java source code and module descriptor as resources in the build outputDirectory. Alternative to declaring a <resource> in the POM with finer filtering as the module descriptor is read to detect sources to be copied.
Implementation: org.codehaus.mojo.gwt.GwtResourcesMojo
Language: java
Bound to phase: process-resources

Vlastimil Ovčáčík
  • 2,799
  • 27
  • 29