How to change jar output directory when running as maven install in Eclipse? - eclipse

I am making a Minecraft plugin for Bukkit 1.8, and everything works fine. I right click on the project name > Run As > Maven install. It outputs the .jar file to the target directory. I then copy the file to the plugins folder of my Minecraft server.
I would like to have it output the jar directly into my plugins folder.

A simple way to do that would be to bind an execution of the maven-antrun-plugin to the install phase. This execution would copy the main artifact to the Minecraft server folder.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${project.build.finalName}.jar"
todir="/path/to/server/plugins" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
(This snippet must be placed inside the <build><plugins> element).
Running mvn clean install (or "Run As... > Maven Install" in Eclipse), Maven will do what you want. ${project.build.directory}/${project.build.finalName}.jar refers to the main artifact present in the build directory (which is target by default). You'll need to update the path to the server in the snippet above.

Related

Having only a JAR Maven Archetype file, how can it be generated a new project?

I'm just starting with Maven 3 for an Scala project in IntelliJ.
I have generated a JAR file following this guide.
I moved archetype.jar to a directory in where I want to create a new project. But my questions are:
Is this file stand-alone? Is it enough? It does not work with the command "mvn archetype:generate"
Is it possible to use the jar file without the intervention of any repository? So I can share it with collegues.
What's the best method for this, I've been reseaching and all the guides are based on repositories only and not in working local. Even the local repositories only consists in xmls files with the id but not the contents.
This is a sort of complex question you are asking...I am going to try summarise what I know and let's see if it helps.
Answers to your questions:
1) If you have just the basic Maven Project structure after you have generated the archtype and so on, if you run maven clean install and the project produces a jar, this in theory should be immediately executable from the command line and standalone.
However, as you add dependencies to your small projects, not all dependencies are automatically built into the standalone jar, sometimes you have to tell maven to bundle them into it.
Maven Shade Plugin - adds all the needed dependencies into your jar
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>MainApp</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>launcher</shadedClassifierName>
</configuration>
</plugin>
</plugins>
</build>
2) You do not need to integrate to any repository, its recommended for wider projects so that you can publish artifacts to your firms Repo for eas of use between developers
3) The easiest method is to create a new project in Intellij itself specifying Maven as the project type and that will give you the default project structure. In the pom file you then specify the build block I pasted above and you are essentially good to go...If you need Dependencies also t run your own code you will need to add them in a Dependencies block.
So, I finally used the following command before generating:
mvn install:install-file -Dfile=<path-to-jar-archetype-file> -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=jar
After executing the command, the jar is installed in your local repo. You can generate the project with for instance IntelliJ or go to the path where you want to generate the project and...
mvn archetype:generate

How to launch browser automatically after war deployment?

I have a war maven project and I want that when I deploy it on the server the default browser (Mozilla in my case) is launched automatically with the default url for access to the main page.
Of course with JBoss EAP6 I have just to run the Maven command clean install jboss-as:deploy to generate the war file and deploy it on the server.
Do I have to add something to the pom.xml or make any configurations in Eclipse?
Use maven's exec plug-in. More information here.
I used this and it works good but juste for the maven phase : install but for the deploy no:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>Run URL in system browser.</id>
<phase>install</phase>
<configuration>
<target>
<exec executable="start" vmlauncher="false">
<arg line="http://localhost:8080/mywarApp" />
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I use eclipse and i made for goals "clean install jboss-as:deploy" into the run configurations. and when in click in , once the install phase is runing the browser is runing too but i want that it will happen when the deploy phase is end of runing , have you any idea

Eclipse + Maven + Groovy: src/test/groovy directory gets removed after update project configuration

I'm developing a Java web project in Eclipse (STS version 2.8.1.RELEASE) with Maven (version 2.2.1) and unit tests written in Groovy. The unit tests are located under src/test/groovy. Furthermore I'm using the m2eclipse plugin for Eclipse (version 1.0) and the Gmaven plugin in Maven (version 1.3).
Building in Maven works without problems: the groovy files are compiled and executed as tests. For the unit tests to work in Eclipse I added the Groovy nature to the project, added the folder src/test/groovy under Configure Build Path... and set the output folder to target/test-classes.
This works until I do an update of the project configuration under Maven -> Update Project Configuration.... After I do this every time the directory src/test/groovy gets removed from the source folders in Eclipse and I have to add it again and set the output directory.
Is there something I am missing or why is Eclipse deleting my source folder configuration every time I do an update of the project configuration?
My GMaven configuration looks as follows:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<configuration>
<providerSelection>1.7</providerSelection>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
Using the builder-helper-maven-plugin helped. Eclipse adds the source folder and sets the output folder correctly. I used the following configuration:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/groovy</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
I had a similar issue, mine was that eclipse was preventing me from writing groovy files into java folder. But you could try the same configuration out, or check out my whole pom at github
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.eclipse.jdt.groovy.core.groovyNature</projectnature>
</additionalProjectnatures>
<!-- Source includes is necessary to allow groovy files in the java
folder, else eclipse will throw a filtering exception -->
<sourceIncludes>
<sourceInclude>**/*.groovy</sourceInclude>
</sourceIncludes>
<!-- Download sources will make maven download and attach source files
where available -->
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
After I put in this configuration in the pom, the .classpath got generated properly.
Check out this issue on SoF here. You need to add the build-helper-maven-plugin to get the resources added.
You need to install the groovy-eclipse configurator for m2eclipse. It is available from this update site:
http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.7/
If you are using m2eclipse v1.0 or later, then choose to install from here:
m2e Configurator for Groovy-Eclipse (Optional)
If you are using an older version of m2eclipse, then install from here:
Groovy-Eclipse m2eclipse integration pre v1.0 (deprecated)
Try by adding your Groovy source directory.

Only copy modified files in maven-war-plugin

I'm currently using maven 3.x with the maven-war-plugin. For developer builds I would like to be able to use the war:exploaded goal, but only copy resources that have changed. Is there a way to do this?
I've been looking through the docs and have not been able to find a way to do this in the current versions, but there used to be (in the old maven 1.x version) a property maven.war.resources.overwrite that would allow this.
Thanks.
I'm not aware of a way to do this using the maven-war-plugin, but if your IDE supports it, you could have the IDE auto-deploy changes. For example, the Web Tools Platform for Eclipse supports this feature for Tomcat. However, if your build process is complex or does something weird before invoking the maven-war-plugin, this may not work for you.
A second option: if you're running Linux, set up rsync to copy recently modified files to your servlet container. A co-worker did this by having the servlet container's web app directory sync with the Eclipse project's output directory, and it worked well (just modify your code, Eclipse will build it automatically, and rsync will copy your changes).
For this purpose I use maven-antrun-plugin
Example:
<project xmlns="http://maven.apache.org/POM/4.0.0"
....
....
<!--DJ: Settings for deploy only-->
<!--DJ: Dir to where copy files-->
<!--DJ: And date when build have been started, to select only modified files in the future-->
<properties>
<tomcat.dir.rootDir>C:\apache-tomcat-6.0.35\webapps\ROOT1</tomcat.dir.rootDir>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>
.....
<!--Ohter dependensies here-->
.....
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copyModifiedFilesFrom_ExplodedWAR_Dir</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Upload new files to dir ${tomcat.dir.rootDir} modified after date ${maven.build.timestamp} "/>
<copy todir="${tomcat.dir.rootDir}">
<fileset dir="${project.build.directory}/${project.build.finalName}" includes="**/*">
<!-- Include only recompiled files -->
<date datetime="${maven.build.timestamp}" pattern="yyyy-MM-dd HH:mm:ss" when="after"/>
<!-- and only *.class files -->
<filename name="**/*.class"/>
</fileset>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
.....
.....
</plugins>
</build>
</project>
Then run maven with params:
mvn pom.xml compile install org.apache.maven.plugins:maven-war-plugin:2.1-alpha-1:exploded
As result only changed files will be recompiled and only recompiled filed will be replaced in tomcat webapp dir.

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!