Is there a way to compile multiple java source directories in a single maven project?
You can add a new source directory with build-helper:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/generated</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I naively do it this way :
<build>
<finalName>osmwse</finalName>
<sourceDirectory>src/main/java, src/interfaces, src/services</sourceDirectory>
</build>
This worked for me
<build>
<sourceDirectory>.</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<includes>
<include>src/main/java/**/*.java</include>
<include>src/main2/java/**/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
To make it work in intelliJ, you can also add generatedSourcesDirectory to the compiler plugin this way:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<generatedSourcesDirectory>src/main/generated</generatedSourcesDirectory>
</configuration>
</plugin>
This also works with maven by defining the resources tag. You can name your src folder names whatever you like.
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.java</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/generated</directory>
<includes>
<include>**/*.java</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
This worked for with maven 3.5.4 and now Intellij Idea see this code as source:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<generatedSourcesDirectory>src/main/generated</generatedSourcesDirectory>
</configuration>
</plugin>
While the answer from evokk is basically correct, it is missing test classes.
You must add test classes with goal add-test-source:
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated/some-test-classes</source>
</sources>
</configuration>
</execution>
Used the build-helper-maven-plugin from the post - and update src/main/generated. And mvn clean compile works on my ../common/src/main/java, or on ../common, so kept the latter. Then yes, confirming that IntelliJ IDEA (ver 10.5.2) level of the compilation failed as David Phillips mentioned.
The issue was that IDEA did not add another source root to the project. Adding it manually solved the issue. It's not nice as editing anything in the project should come from maven and not from direct editing of IDEA's project options. Yet I will be able to live with it until they support build-helper-maven-plugin directly such that it will auto add the sources.
Then needed another workaround to make this work though. Since each time IDEA re-imported maven settings after a pom change me newly added source was kept on module, yet it lost it's Source Folders selections and was useless. So for IDEA - need to set these once:
Select - Project Settings / Maven / Importing / keep source and test
folders on reimport.
Add - Project Structure / Project Settings / Modules / {Module} / Sources / Add Content Root.
Now keeping those folders on import is not the best practice in the world either, ..., but giving it a try.
This can be done in two steps:
For each source directory you should create own module.
In all modules you should specify the same build directory: ${build.directory}
If you work with started Jetty (jetty:run), then recompilation of any class in any module (with Maven, IDEA or Eclipse) will lead to Jetty's restart. The same behavior you'll get for modified resources.
In the configuration, you can use <compileSourceRoots>.
oal: org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-cli)
[DEBUG] Style: Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<basedir default-value="${basedir}"/>
<buildDirectory default-value="${project.build.directory}"/>
<compilePath default-value="${project.compileClasspathElements}"/>
<compileSourceRoots default-value="${project.compileSourceRoots}"/>
<compilerId default-value="javac">${maven.compiler.compilerId}</compilerId>
<compilerReuseStrategy default-value="${reuseCreated}">${maven.compiler.compilerReuseStrategy}</compilerReuseStrategy>
<compilerVersion>${maven.compiler.compilerVersion}</compilerVersion>
<debug default-value="true">${maven.compiler.debug}</debug>
<debuglevel>${maven.compiler.debuglevel}</debuglevel>
<encoding default-value="${project.build.sourceEncoding}">${encoding}</encoding>
<executable>${maven.compiler.executable}</executable>
<failOnError default-value="true">${maven.compiler.failOnError}</failOnError>
<failOnWarning default-value="false">${maven.compiler.failOnWarning}</failOnWarning>
<forceJavacCompilerUse default-value="false">${maven.compiler.forceJavacCompilerUse}</forceJavacCompilerUse>
<fork default-value="false">${maven.compiler.fork}</fork>
<generatedSourcesDirectory default-value="${project.build.directory}/generated-sources/annotations"/>
<maxmem>${maven.compiler.maxmem}</maxmem>
<meminitial>${maven.compiler.meminitial}</meminitial>
<mojoExecution default-value="${mojoExecution}"/>
<optimize default-value="false">${maven.compiler.optimize}</optimize>
<outputDirectory default-value="${project.build.outputDirectory}"/>
<parameters default-value="false">${maven.compiler.parameters}</parameters>
<project default-value="${project}"/>
<projectArtifact default-value="${project.artifact}"/>
<release>${maven.compiler.release}</release>
<session default-value="${session}"/>
<showDeprecation default-value="false">${maven.compiler.showDeprecation}</showDeprecation>
<showWarnings default-value="false">${maven.compiler.showWarnings}</showWarnings>
<skipMain>${maven.main.skip}</skipMain>
<skipMultiThreadWarning default-value="false">${maven.compiler.skipMultiThreadWarning}</skipMultiThreadWarning>
<source default-value="1.6">${maven.compiler.source}</source>
<staleMillis default-value="0">${lastModGranularityMs}</staleMillis>
<target default-value="1.6">${maven.compiler.target}</target>
<useIncrementalCompilation default-value="true">${maven.compiler.useIncrementalCompilation}</useIncrementalCompilation>
<verbose default-value="false">${maven.compiler.verbose}</verbose>
</configuration>
these are all the configurations available for 3.8.1 version of compiler plugin. Different versions have different configurations which you can find by running your code with -X after the general mvn command. Like
mvn clean install -X
mvn compiler:compile -X
and search with id or goal or plugin name
This may help with other plugins too. Eclipse, intelliJ may not show all configurations as suggestions.
Related
I have some test related classes that I want to exclude from the compiled jar output for a project. This is a legacy project and I don't want to lose existing revision history by moving the classes to src/test. Since I am already using build-helper-maven-plugin I thought I would be able to specify an exclusion pattern there, but so far nothing I have tried seems to work. I run
mvn clean install package
in my project root and I see the log message
[INFO] --- build-helper-maven-plugin:3.0.0:add-source (add-source) #
Person-ejb --- [INFO] Source directory:
/media/psf/Home/Documents/workspace/optics/optics/Person/ejb/src
added.
but when I look at the compiled jar it still containts the test directory and its contents. Any idea what I could be doing wrong?
My pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/</source>
</sources>
<excludes>
<exclude>**/test/**</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>add-reource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>resources/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Context
I believe this is a bug in build-helper-maven-plugin or possibly a misprint in the usage documentation. The <excludes> tag is completely ignored by the plugin.
It does not even appear in the code completion in Eclipse
Solution
While you can't exclude files in the builder-helper-maven-plugin, you can exclude files in the maven-compiler-plugin. So if you simply add the following configuration to your maven-compiler-plugin it should exclude the **/test/** directories
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<excludes>
<exclude>**/test/**</exclude>
</excludes>
</configuration>
</plugin>
Best Practice
I feel obligated to remind the reader that this is an anti-pattern in maven. Ideally you would create separate pom files for each source directory, then add them as separate modules in your parent pom file. I realize this may be a larger refactoring effort though
Last week our team decided to move from Netbeans to Eclipse, cause we develop some new plugins that can run only on Eclipse.
At first the project start using my maven plugin from pom <build> tag. But the stop doesn't work. In netbeans I used exec-maven-plugin to exec, java -jar Project.jar from the target folder. Here is the code:
<build>
<sourceDirectory>src/java</sourceDirectory>
<resources>
<resource>
<directory>src/cp</directory>
<targetPath>src/cp</targetPath>
</resource>
<resource>
<directory>src/rpt</directory>
<targetPath>src/rpt</targetPath>
</resource>
<resource>
<directory>src/sql</directory>
<targetPath>src/sql</targetPath>
</resource>
<resource>
<directory>etc</directory>
<targetPath>etc</targetPath>
</resource>
<resource>
<directory>www</directory>
<targetPath>www</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.4</version>
<configuration>
<connectionType>connection</connectionType>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<id>setVersion</id>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>sh</executable>
<arguments>
<argument>./etc/changeVersion.sh</argument>
</arguments>
<workingDirectory>${basedir}</workingDirectory>
</configuration>
</execution>
<execution>
<id>debugJar</id>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cd target/</executable>
<executable>java</executable>
<arguments>
<argument>-Xdebug</argument>
<argument>-Xnoagent</argument>
<argument>-Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address}</argument>
<argument>-jar </argument>
<argument>${project.artifactId}-${project.version}.jar</argument>
</arguments>
<workingDirectory>${project.build.directory}</workingDirectory>
</configuration>
</execution>
<execution>
<id>runJar</id>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cd target/</executable>
<executable>java</executable>
<arguments>
<argument>-jar </argument>
<argument>${project.artifactId}-${project.version}.jar</argument>
</arguments>
<workingDirectory>${project.build.directory}</workingDirectory>
</configuration>
</execution></executions>
</plugin><plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestEntries>
<Project-Name>${project.artifactId}</Project-Name>
<!-- Uncomment if you want to make changes and/or debug in Entuito-->
<!--<Class-Path>${local.OurFramework.dir}${OurFramework.jar}</Class-Path>-->
</manifestEntries>
<manifest>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<mainClass>${project.groupId}.Mammut</mainClass>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
<extensions>
<!-- Enabling the use of SSH -->
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ftp</artifactId>
<version>2.10</version>
</extension>
</extensions>
In Netbeans, when we used this build the project had no problem for Debug and Run using Netbeans controls buttons. But in Eclipse the project run and stop button doesn't stop the project. The proccess java -jar Project.jar is still running and we must use htop or other methods to kill it.
Cause of the copying for all of the libs and packaging, are slow proccess and used many read/write operation, we want to Run and Debug the projects using the Java Application run, and to exclude the need of rebuild the whole project for every minor change. But the problem occurs here too. Every project have dependency that is our java framework and all of the framework resource files are inside the jar archive (some conf.xml files, images and etc.) At most of the time when need to fix our to change something we have both project open in one Workspace. Netbeans and Eclipse automatically detect the dependency location and change it from .m2 folder to the workspace project that is open at the moment.
The problem here is that when I use maven clean install for the framework and then run the main project using JavaApp the project doesn't run cause can't find the resource files from the framework. At this time if I close the frameWork from the workspace and run the main project Eclipse change the dependency path to .m2 folder and everything is fine, but this is very slowly proccess and makes the debug unpossible.
Can anyone share a pom build that can help me for even one of the problem or any advices and guide "how to" done it properly if my methods are wrong.
We handled the problem with missing resources when the project and it dependancy is open. Their was a bug in our code for loading the resoure files when they aren't in .jar archive.
The only thing now that left is this little problem with the Manifest file. There is no manifest file inside project/target/classes/ is there a way to create the same Manifest file like that one from inside the packaged jar.
I would like to generate a sources jar file for my project, so I have included the maven-source-plugin. However, I am also using the resource filtering plugin to set a version number in a property file for my project. When I generate a final jar file, the property file has been filtered and the version is set. However in the sources jar, is still has the unfiltered property. I would like for the sources plugin to also invoke the resource filtering. How can I do this?
Here is (part) of my pom.xml
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
and here is the property file that I want filtered in
version = ${project.version}
EDIT
For clarification, the root of my issue is that I have another project that is a GWT project built using this library. Part of the requirements of a GWT project is that the source code has to also be made available for anything that is going to be compiled to client side javascript. Therefore, this project contains both the compiled jar and the sources jar in the classpath.
So there are now two properties files with the same package path and name, one exists in the compiled jar and one in the sources jar.
When I attempt to read this file, it seems to pick the properties file out of the sources jar, which has not been filtered.
Normally, you'd use the maven-source-plugin for this. However, I see in its documentation that you cannot remove src/main/resources from its processing, while simultaneously adding target/classes to its processing cycle (which is what you would need to do in order to accomplish your task)
Therefore, I think your best bet is through a maven-assembly-plugin configuration.
I'm trying to setup my project to use gwt maven plugin. Its compiling properly but I'm not able to use either the dev mode or super dev mode for development.
My settings are as follows:
Maven configurations in order
mvn clean install
mvn tomcat7:run-war-only
mvn gwt:run-codeserver
GWT Version: 2.6.1
IDE: Intellij 14 Community Edition
When I make changes to client java files and click the "compile" button on the code server page, they're not reflected on the webpage. I suspect the code server is not looking at the same sources I'm changing. Specifically i think its looking for sources to compile in target/{project-name}/*
Following is the snippet of the POM file I'm using.
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<!--compilerArgument>-proc:none</compilerArgument-->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<path>${tomcat.context}</path>
<port>${tomcat.webport}</port>
<ajpPort>${tomcat.ajpport}</ajpPort>
<contextReloadable>true</contextReloadable>
</configuration>
</plugin>
<!-- GWT Maven Plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>${maven.gwt.plugin.version}</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<draftCompile>true</draftCompile>
<hostedWebapp>${webappDirectory}</hostedWebapp>
<noServer>true</noServer>
<port>${tomcat.webport}</port>
<runTarget>${tomcat.context}/index.html</runTarget>
<!--codeServerWorkDir>${webappDirectory}</codeServerWorkDir-->
<copyWebapp>true</copyWebapp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
<configuration>
<webappDirectory>${webappDirectory}</webappDirectory>
</configuration>
</plugin>
</plugins>
</build>
Any help would be appreciated!!
Because you list src/main/java as a <resource>, its files are copied to the ${project.build.outputDirectory}, and that one comes first in the classpath because you could filter files from <resource> and have <source> and <resource> intersect (which is the case here). See http://jira.codehaus.org/browse/MGWT-290
So either:
remove src/main/java from project resources
remove *.java files from project resources (so at least they're not copied over to the output directory and don't shadow src/main/java)
or run mvn resources:resources each time you change files in src/main/java (like you have to do with files in src/main/resources, because of possible filtering); this can be automated by your IDE or some other tool (e.g. watchman) though.
I would upgrade the project to use gwt-2.7.0 and gwt-maven-2.7.0, then nothing special is needed to run superdev mode among your app in a servlet container, just run mvn gwt:run and point your browser to http://localhost:8888, then each time you change your code just hit refresh in your browser to recompile the app.
As you can see it's pretty simpler, and you would take advance of recompiling in 2.7.0 is a lot faster.
Having tried all possible forums' advice and diverse codeServer launch parameters unsuccessfully, the only means I have found to trigger a refresh without restarting any server is to click again on the bookmark 'Dev Mode Off' followed by 'Dev Mode On' then 'Compile'button ... and the code Server recompiles the changed java source incrementally! (the Dev Mode shortcuts are those you should have dragged from the code server home page like http://localhost:9876 into the browser bookmarks).
A page refresh was enough with "Google Plugin for Eclipse" ( ==GPE, deprecated Jan2018) and -as it seems- no longer with "GWT Eclipse Plugin" (replacing GPE) (appreciate the subtle difference in wordings!)
I have a maven project which uses wsgen to generate XSD files from the compiled java classes.
The problem is that I want to add the generated files to the jar as resources. But since the resource phase runs before the process-classes phase, I can't add them.
Is there a way to tell maven to add additional resources that were generated at the process-classes phase?
I would suggest to define the output directory for the XSD files into target/classes (may be with a supplemental sub folder which will be packaged later during the package phase into the jar. This can be achieved by using the maven-resources-plugin.
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-classes</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>${basedir}/target/xsd-out</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
You need to take care that the resources plugin is positioned after the plugin which is used to call the wsgen part. You can also use the prepare-package phase instead to make sure the resources will be correctly packaged.