Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly include Java sources in Maven?

Tags:

maven

gwt

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?

like image 801
Laurent T Avatar asked Jan 04 '12 16:01

Laurent T


People also ask

What is groupId and artifactId in Maven project example?

groupId – a unique base name of the company or group that created the project. artifactId – a unique name of the project. version – a version of the project. packaging – a packaging method (e.g. WAR/JAR/ZIP)

How do I create a Maven source?

If you prefer to use the Maven command prompt, simply run the command mvn clean generate-sources or mvn clean generate-test-sources (if using test resoures for PageYaml) from your project directory. If you would like to do this from Eclipse, right click your project and choose Maven > Update Project configuration.

What is sources jar Maven?

The “maven-source” plugin is used to pack your source code and deploy along with your project. This is extremely useful, for developers who use your deployed project and also want to attach your source code for debugging.


1 Answers

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>
like image 165
Sean Patrick Floyd Avatar answered Oct 14 '22 15:10

Sean Patrick Floyd