What is the correct way to specify a main class when packaging a jar using m2eclipse? - eclipse

Problem:
I'd like to specify the main class in a jar file that I am packaging using m2eclipse: [right-click] -> Run As -> Maven package. I'm still learning Maven and from what I've read, the straight-up way of accomplishing this task is to add a stanza to the pom.xml.
Here are examples I found when I was researched this issue:
Cannot make executable jar using m2eclipse
http://docs.codehaus.org/pages/viewpage.action?pageId=72602
My question is this: is it okay to manually edit the pom.xml file outside of Eclipse/m2eclipse using a text editor, or should I be doing this configuration using the m2ecplise GUI? There are several tabbed dialog boxes that seem like they might be likely candidates for this task, like "Plugins" and "Build". I looked through Sonatype's documentation and couldn't find any detailed instructions on how to accomplish what I need to do.
I'm a little hesitant to edit the pom.xml manually because I notice the "Effective POM" already has a lot of extra stuff in it, including the plugin definition that needs to have added to it:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
If I understand correctly, the Effective POM needs to be changed so that the plugin is configured like this:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>[name of main class]</mainClass>
<packageName>[package name]</packageName>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${pom.url}</url>
</manifestEntries>
</archive>
</configuration>
</plugin>
Is this right? And if so, do I do this through m2eclipse or should I just copy all the extra Effective POM stuff and paste it into the actual pom.xml using a text editor?
Thanks to anyone who can shed some light.
UPDATE: I went ahead and manually edited the pom.xml file in a text editor. When I viewed Effective POM in m2eclipse it displayed everything that I put in (I assume). I built the jar and the main class was correctly set.
This seems like a hack to me though. Does anyone know if there's a way to do this configuration using m2eclipse itself? I checked the m2eclipse tabs and nothing seemed to have been updated as a result of my manual edits of pom.xml (other than the Effective POM tab).

m2eclipse doesn't do everything for you, i.e. there isn't a pretty UI tab for handling everything.
My team is most comfortable with editing the POM manually, and using the other tabs for verification (like Effective POM view).

Related

How to use build-helper-maven-plugin to create extra directory?

I have a Java Maven project with the following structure. (This is a logical structure, not necessarily the exact directory structure.)
Project
|-Submodule
| |-src/main/java
| |-src/main/resources
| |-src/test/java
| |-src/test/resources
| `-pom.xml
`-pom.xml
In accordance with (my understanding of) Maven standards, the parent pom references the submodule in it's <modules> tag, and the submodule pom references the parent pom as it's <parent>.
Now, my goal is to use Maven to add two new source folders to the submodule, namely src/integrationTest/java and src/integrationTest/resources. I want Maven to "know about" these two folders so that I can execute the tests therein using my Maven testing plugin, maven-pmd-plugin. And I want Eclipse to "know about" these two folders so that they display correctly in Eclipse's graphical Project Explorer.
I was told that the build-helper-maven-plugin plugin could be used to create these additional directories that I needed. So I added the following configuration to my submodule pom:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-integration-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/integration-test/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-integration-test-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/integration-test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
When I rebuilt the project, Maven didn't generate the directories I wanted. So I took the plugin configuration out of my submodule pom and put it in my parent pom instead. Maven still didn't generate my directories.
At this point, I don't know how to get this plugin working. I'm trying to follow other online tutorials, but I must be doing something wrong. Could anyone explain which pom file is intended to configure build-helper-maven-plugin? And how do I configure Eclipse to display the new Maven project structure correctly?
Thanks for the help!
khmarbaise's comment made me realize my mistake, although I didn't take his advice.
The build-helper-maven-plugin configuration should go in the submodule pom (not the parent pom) where I originally had it. But Maven will not create these directories for you, you create the directories and then Maven uses them.
Once Maven is using your new directories, Eclipse displays them just fine.

Spring REST Docs maven continuous build?

I was going through this tutorial https://www.youtube.com/watch?v=k5ncCJBarRI&t=1443s
Around 1:07:30 the author mentioned about "Gradle has continuous build" later on was able to detect changes in the test and automatically regenerate asciidoc. I was wondering if anyone knows how to set this up in maven?
I have looked through docs in spring and asciidoctor plugin, but was not able to find anything related to this.
I was able to get maven to re-render html when ever there is a change in index.adoc by changing <goal> from process-asciidoc to auto-refresh. However, this does not watch the change in the Test.
Question
Is there a way to tell Maven to watch our test files and re-compile when changes are made?
POM.XML
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.7.1</version>
<executions>
<execution>
<id>generate-docs</id>
<phase>prepare-package</phase>
<goals>
<goal>auto-refresh</goal>
</goals>
<configuration>
<sourceDocumentName>index.adoc</sourceDocumentName>
<backend>html</backend>
<attributes>
<snippets>${project.build.directory}/generated-snippets</snippets>
</attributes>
</configuration>
</execution>
</executions>
</plugin>
Thank you.
This is not a continuous build solution but it works similarly. However, the process does take some time because it essentially re-packages the project everytime there is a change... May not be ideal for some use cases...
I found a plugin that watches files. https://github.com/fizzed/maven-plugins
Change the watch directory to where your test files. Changed the goal from compile to package.
Watcher will execute mvnw: package when a change is detected. Then the asciidoctor maven plugin will re-package the project.
Add this to your plugin
<plugin>
<groupId>com.fizzed</groupId>
<artifactId>fizzed-watcher-maven-plugin</artifactId>
<version>1.0.6</version>
<configuration>
<touchFile>target/classes/watcher.txt</touchFile>
<watches>
<watch>
<directory><directory>src/test/[your test package]</directory></directory>
</watch>
</watches>
<goals>
<goal>package</goal>
<!-- <goal>compile</goal> -->
</goals>
<profiles>
<profile>optional-profile-to-activate</profile>
</profiles>
</configuration>
</plugin>
Maven does not have an equivalent of Gradle's continuous build. If you want changes in the tests to be detected and to trigger recompilation of the tests and execution of all of the tasks that depend (directly or indirectly) on the compiled test classes, you'll have to use Gradle.

How to tell Maven to update manifest's Bundle-Version?

I'm building Eclipse's plugins using Maven, and I'm looking for a way to tell Maven to update the Bundle-Version in the META-INF\MANIFEST.MF file.
The trick is, I need a clean way to do it. So that further colleagues (or even me in some months/years) may not break the final automated build.
Here is what I tried :
Using "Bundle-Version: ${project.version}" in the MANIFEST.MF
On a maven point-of-view, this is working.
BUT my Eclipse project is constantly displaying an error : The specified version does not have the correct format (major.minor.micro.qualifier)
Using the above but in another META-INF folder (named META-INF-MAVEN) that is used by maven instead of the original :
<resources>
<resource>
<directory>META-INF-MAVEN</directory>
<targetPath>META-INF</targetPath>
<filtering>true</filtering>
</resource>
</resources>
This is still "working", but the needed duplication of MANIFEST.MF file is far away from what I'm looking for. This is a large trap for future errors, like editing the original MANIFEST.MF file and forgetting the maven one, resulting in a bugged release.
Using org.apache.maven.plugins to add the Bundle-Version tag at compile-time from the pom file :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>target/classes/META-INF/MANIFEST.MF</manifestFile>
<manifestEntries>
<Bundle-Version>${project.version}</Bundle-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
This is the most promising way, BUT it only works if the original MANIFEST.MF does not have a Bundle-Version line.
The result is, again, an error within the Eclipse project.
And so, here I am, asking for any idea.
Thanks.
Following solution finds the Bundle-Version: line in META-INF/MANIFEST.MF and replaces it with the Maven project version. replacer maven plugin was used for that but there are certainly other maven plugins that can do the same work like find-and-replace-maven-plugin.
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>update-manifest-version</id>
<phase>process-resources</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<file>${basedir}/META-INF/MANIFEST.MF</file>
<regex>true</regex>
<replacements>
<replacement>
<token>Bundle-Version: .*</token>
<value>Bundle-Version: ${project.artifact.selectedVersion.majorVersion}.${project.artifact.selectedVersion.minorVersion}.${project.artifact.selectedVersion.incrementalVersion}.qualifier
</value>
</replacement>
</replacements>
</configuration>
</execution>
</executions>
</plugin>

How to copy dependencies to gae war/WEB-INF/lib

I'm coming from Ant perspective, so pardon me. I realise there are quite a few questions here already on how to maven dependencies, but none of them seem to tell how to do what need to do.
Question 1:
Currently, in conjunction with using maven-war-plugin, when I run mvn war:war, it creates a war folder in target folder.
However, I wish copy all the dependencies' jars to war/WEB-INF/lib set up by google eclipse plugin (with gae enabled, gwt disabled), without overwriting the jars that google eclipse plugin placed there.
I don't wish to setup a war file or war directory. I just need to copy/consolidate all the non-gae jars with the gae jars so that when the project is run as a gae web app, Eclipse would not complain ClassNotFoundException.
Question 2:
When using Ant in Eclipse, I could run Ant targets within Eclipse.
Right now, I have to perform mvn commands from a shell window (which is mutually oblivious to the existence of an Eclipse session). It appears that the only thing that is automatically done is whenever I update dependencies.
Is there a way, or any plugin for eclipse that would allow me to run mvn goals within Eclipse?
Additional info:
mvn dependency:copy-dependencies persists in copying to target/dependency directory, with the following:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/war/WEB-INF/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeArtifactIds>gwt-user,gwt-dev</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
I even tried changing to absolute path
<outputDirectory>
/home/geek/eclipse/workspace/Demo-autoshoppe/holycow
</outputDirectory>
But holycow directory is still empty and mvn still persists in copying to target/dependency directory. My current solution is to softlink target/dependency as war/WEB-INF/lib, which is a very very bad kludge. Why is maven not sensitive to outputDirectory specification? I am using Ubuntu's maven 2.2.
I have the real answer for you, my man.
Use the "default-cli" execution id. Make sure you're using Maven 2.2+. This exec-id applies to command-line executions of the mojo.
<build>
<pluginManagement>
<plugins>
<!-- Copy dependencies to war/WEB-INF/lib for GAE proj compliance. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/war/WEB-INF/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeArtifactIds>gwt-user,gwt-dev</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Cheers.
An associate emailed me this answer which works. Trigger the follow through
mvn build or mvn package
but not directly thro mvn dependency:copy-dependencies.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/war/WEB-INF/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeArtifactIds>gwt-user,gwt-dev</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
Regarding #1: I'd suggest creating your maven project based off this plugin and archetype http://code.google.com/p/maven-gae-plugin/ (see the GWT based example if you are writing a GWT app for GAE).
Regarding #2: Check out m2eclipse plugin for full maven integration in eclipse. It's written by Sonatype (the creators of maven): http://m2eclipse.sonatype.org/

How do I get Eclipse to resolve classes generated with Maven 2?

I'm using Google Protocol Buffers to generate some Java classes for my project. Using Maven 2 and its "antrun" plugin, these classes are freshly generated before compile, output to target/generated-sources and put on the classpath during the build. So building the project from the POM is no problem.
However, Eclipse doesn't know how to resolve the generated class, because the folder it's in doesn't seem to be on the IDE's classpath during development. I'm using m2eclipse and have it manage dependencies for me, so I had expected Maven to take care of this.
How can I get IDE support (code completion etc.) for the generated code?
m2eclipse supports this. First, add the path to your build path:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/java/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Second, add support for that to m2e:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<versionRange>[1.0,)</versionRange>
<goals>
<goal>parse-version</goal>
<goal>add-source</goal>
<goal>maven-version</goal>
<goal>add-resource</goal>
<goal>add-test-resource</goal>
<goal>add-test-source</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnConfiguration>true</runOnConfiguration>
<runOnIncremental>true</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
The second step might not be necessary, if your eclipse installation has installed the "org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml" plugin. This plugin is available via Window -> Preferences -> Maven -> Discovery. Currently, that does not work here at Eclipse Kepler, therefore, I fetched the JAR (linked from the xml shown in the Catalog URL) and extracted the fragments from org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml by hand.
m2eclipse doesn't support this. You must manually add the folder target/generated-sources as a source folder. When you tell m2eclipse to "Update Project Configuration", this will be overwritten and you have to restore it.
Also, make sure that Eclipse looks for changes in the workspace.
There might be some issues, though. Eventually, you'll run into errors that some class can't be compiled because some other class can't be resolved. Code completion will work, though. The root cause of this issue is that Eclipse gets confused when Maven changes class files in target.
To solve this, you must tell Eclipse to compile to a different place than Maven.
What you should see in your project explorer is a container named "Maven Dependencies" in place of the usual "Referenced libraries". This means m2eclipse is managing your build path.
In my case, to achieve this, I checked "Include Modules" and unchecked "Skip Maven compiler plugin when processing resources" on the "Maven" section of Project->Properties.
Personally I resolved this problem by setting up the generated classes as a seperate project and made it a dependency in my main (non-generated) project. I was using wsdl2java to generate webservice classes so the "source" in my sub-project was the wdsl and xsds. Worked well even when the wsdl was changing regularly.
I had this issue with code generated using Maven and wsdl2java and here's what I did in Eclipse Juno to resolve it. Assume my project is named project1:
Right-click project1 and select Properties
Choose Java Build Path from the left and select the Libraries tab
Click Add Class Folder
Select the bin directory and click OK (project1/target/generated-sources/bin)
Click OK and Refresh the project
As an added bonus you can also attach the source code:
Click the arrow next to the new class folder you just created
Click on Source attachment
Click the Edit button
Set the Path to /project1/target/generated-sources/axis2/src
Click OK
Right-click project and select Properties
Choose Java Build Pathfrom the left and select the Source tab
Click Add Folder
Select the bin directory and click OK
(project/target/generated-sources/xxxx) Click OK and Refresh the project
How can I get IDE support (code completion etc.) for the generated code?
Typically I would add the m2e lifecycle-mapping plugin to the pom.xml file as described in #koppor's answer. However adding per-eclipse code to my pom.xml files is not an option at work which is mostly an IntelliJ shop.
My solution first adds the build-helper-maven-plugin to the pom.xml which works fine from the command line but not in eclipse.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
To fix eclipse I installed the Apt M2E Connector from the Eclipse Marketplace. I think things started working right after I restarted and then rebuilt all of my projects. I now see the following in my source dirs:
src/main/java
target/generated-sources
...
Feature!
Did you try to refresh the Eclipse project?
(source: oyvindhauge.com)
When an external tool generate new files or updates old ones, Eclipse will not be able to detect the change until the next request.
Another option would be to define a new Custom builder, specifying for that builder to "refresh resources upon completion":
alt text http://www.cs.lth.se/EDA180/2005/Verktyg/eclipse_refresh.gif
Worked for me (But you will to have to follow this every time so you can add this path in pom.xml)
Right click on your project > Build Path > Configure Build Path
In sources tag, click on [Add Folder] button
Check target/generated-sources/annotations
To generate Java source files from .proto files use Protocol Buffers Plugin which works out-of-the-box in eclipse Oxygen.
Basic usage (see here for detailed description):
make sure that native protoc compiler is installed on your system
update your pom.xml file:
make sure you use at least Java 6 (Java 7+ is recommended)
add plugin invocation
add the corresponding dependency for com.google.protobuf:protobuf-java
put your .proto files inside project's src/main/proto directory
update the project (via Maven -> Update project...)
Example pom.xml:
<project>
...
<build>
<plugins>
<!-- Require at least Java 6 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- Generate .java files from .proto definitions -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocExecutable>/usr/local/bin/protoc</protocExecutable>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.5.1</version>
</dependency>
...
</dependencies>
...
</project>
Some additional notes:
if protoc executable is in the PATH the protocExecutable configuration entry can be omitted
test-only protobuf message definitions can be put into project's src/test/proto directory
I recommend installing Protocol Buffer Descriptor Editor (marketplace link)
Good luck!