I want to use maven for a GAE project, how do i do? - eclipse

i'm new in the world of GAE. I 'm using eclipse and GAE's SDK, i can deploy to the cloud with the GAE icon and everything is fine. The problem arises when I have to import the infinite number of dependency, then I want to use maven for that. I discovered that there is a special GAE maven plugin called:
maven-gae-plugin
Can I use regular maven only to fetch dependencys or I have to use the GAE special plugin to do this?
Thanks

You'll need to define a pom.xml for the project which declares the dependencies.
When you build with maven the dependencies will be downloaded from the remote repositories and stored in you local repository ${userhome}/.m2/repository.
The maven build will also bindle the dependencies in your war file.
The easiest way to get started is by creating a project structure using an archetype.
There are 2 archetypes that I've tried for gae so far:
gae-archetype-gwt, which is built adjacent to the gae-maven-plugin, see this article.
gae-eclipse-maven-archetype, see this article and also note the link at the top of the article for the helios update.
As the name suggests gae-eclipse-maven-archetype has better support for eclipse, I have been finding that the configurations for maven and eclipse have been clashing with each other, which gae-eclipse-maven-archetype goes a long way to alleviate.
If your current project is not using the maven directory structure, then you are going to have an uphill battle. Maven projects are easier to configure if you try to fit in with the defaults which are largely sensible options, rather than going against the grain and having to override all of the default configuration options.

There is no reason you can't use maven for it's dependency management only. The GAE dependencies are all in maven central.
There is a write up about how to set it up here
I personally use the eclipse plugin in dev and the maven plugin when running under continuous integration.
The main gotcha is to follow the advice about ensuring that the maven dependencies are the last thing in your build path under the GAE plugin dependencies.

Add the following to your pom.xml, modifying it to your needs:
<project>
...
<properties>
...
<com.google.appengine-version>1.6.4</com.google.appengine-version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${com.google.appengine-version}</version>
</dependency>
...
</dependencies>
...
</project>
In case you need any additional GAE-related artifacts besides appengine-api-1.0-sdk, have a look for those artifacts in The Central Repository under com.google.appengine, then add them to your pom.xml's dependencies list.

Related

How to set Maven dependencies on a local jar file

I am starting to play around with Maven, to see whether we could use it in the future to handle our dependency management, and IDE environments.
I have looked at some YouTube vids on how to get started with Eclipse (we also use Eclipse), and where you basically start off with creating a new project of type Maven. I have done this, and imported my existing source into the src/main package type.
Now I want to start adding the dependencies. No changes to my pom file yet.
I have two directories with jar files in them, and I need to set those dependencies in the pom file.
How do I do that?
This is not how you usually use Maven. You can add a jar through a path
<dependency>
<groupId>org.javap.web</groupId>
<artifactId>testRunWrapper</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/testRunWrapper.jar</systemPath>
</dependency>
but the recommended way is to draw your jars from a Maven repository (like MavenCentral, or your Nexus/Artifactory).
So if you want to use Maven in your company, make sure you have a running Nexus or Artifactory server in your company as well. Then you can either proxy external Maven repositories (which contain most of the available open source components) or upload your own jars through the interface of your Nexus/Artifactory.

Configuring to Maven Project

I was using eclipse for building my project. Now I am configuring the project to Maven project. All the libs were manually downloaded to lib folder. If I do a Maven build, Maven is unable to map those libs. There are lots of libs, I don't want to manually place them in pom.xml. What is the best way out?
I have read few answers, first declare local repo and then add the respective dependencies. But again I don't want to add all the dependencies manually.
Maven: best way of linking custom external JAR to my project?
1.) Install locally
There are two scopes which are interesting for you. If you declare a scope "runtime" (which is the default scope) and have a local repository configured, maven will try to download the file from your local repository.
2.) System dependency
If you just don't want maven to manage dependencies, you can (although shan't) use the system scope like this:
<dependency>
...
<scope>system</scope>
<systemPath>/path/to/dependency.jar</systemPath>
</dependency>
The downsides are:
the system scope is deprecated and might not work in future versions of maven.
you need to check in a jar file, which is not a good idea.
it may not be portable (e.g. architecture dependent libraries).

How can I use Eclipse p2 repositories from Maven?

I am trying to create an Eclipse based setup where Eclipse projects are Maven based. So it should all work with Maven whether or not Eclipse is used.
I have a dependencies on various Eclipse project libraries, with more to be added. I want to use p2 repositories, and I've managed to pull an Eclipse EMF library and turn it into a jar following this example: Use dependencies from Eclipse p2 repository in a regular Maven build?
The problem is, I could not find a way of streamlining the process. I'd need to manually install the re-packaged dependency from the question given above to local Maven repository so that I can reference that in other projects. I'd like to seamlessly integrate artefacts from p2 repositories to my Maven based setup. m4e does not look like the smooth solution I'm looking for: Ideally I'd like to distribute a set of directories which would do everything in response to a simple mvn clean install : pull libraries from p2 repo, pull other libraries from Maven repositories etc..
Is this doable via Maven and Tycho integration?
Update: first, clarification to the question: just being able to reference to P2 repositories does not help with the scenario where this reference needs to be used from another project. The library (or libraries) referenced from P2 repository must be re-packaged as a jar so that it can be referenced by other Maven projects. The referenced question does the packaging. However, it does not explain how this repackaged output (assembly) can be used from other projects. In my case, this turned out to be referencing the assembly from an aggregating POM, and inheriting form that POM for all projects that would like to use the library with the P2 repository origin.
Tycho projects can pull their dependencies from both p2 repositories and Maven repositories (see this related answer). This could be a solution for you, even if you are not building for an OSGi runtime: Most OSGi bundles also work as "plain" JARs on the classpath.
Limitation: The artifacts referenced from Maven repositories also have to be OSGi bundles, so that Tycho considers them for dependency resolution. If this is not the case (and you can't find replacements which are OSGi bundles), you may be able to combine Tycho's dependency resolution with plain Maven plug-ins:
Use one of Tycho's packaging types (e.g. eclipse-feature) and specify the dependencies to the p2 artifacts in the file format for the packaging type (e.g. a feature.xml)
Additionally configure the plain Maven goals in your POM. Tycho injects the OSGi/p2 dependencies into the Maven model at runtime, so for example a maven-compiler-plugin:compile call would see both the Maven dependencies and the p2 dependencies.
The solution is to create a multi module setup with Maven, and declare a dependency on the outputs of EMF library re-packaging (from the question I've referenced) The parent pom for all projects has this:
<dependencies>
<dependency>
<groupId>com.mymodule</groupId>
<artifactId>myartifact</artifactId>
<version>0.0.1</version>
<classifier>repackaged</classifier>
</dependency>
</dependencies>
<modules>
<module>../mymodule</module>
</modules>
Which lets all modules that has this module as parents access the repackaged P2 artifacts.

How should you load Spring related jar in Eclipse?

I new with spring and is following the example from "Spring in Action 3rd Edition".
I want to run the code from the example, so I copied the code.
I install Spring STS suite and have a test spring project. It seems it doesn't include spring's jar implicitly so I need to configure the build path and include and jar one by one.
And jar is in some strange location too (I think they are installed by Spring STS, although I have no idea whether it include Spring itself).
And the spring core depends on common logging from apache:
And I need to go to apache common logging site to download the jar and put it in the lib folder of the project, then set it in the build path.
The whole process is unbearable. What if spring got 20 jars? Is there other way to do this?
Thanks all.
To ease the pain of getting the dependencies, it's highly recommended that you use Maven.
All you need to get started is the following :
Checkout this 5 minute start for Apache Maven.
I have a 'Helloworld' Spring + Maven project (specifically to work with Spring In Action, I might add) setup on Git Hub which should get you started without any hassle.
If you are familiar with GIT then fork this repository otherwise,
Download the whole project as a zip/tarball from here.
This project can also be used as the starting point for a Spring app. Read more about how to get the Spring dependencies using Maven here.
Once you do that a mvn clean install inside the project directory is all you need to get all the required dependencies and there is no manual mucking about to get the jars, put them in the classpath and so on and so forth.
There should be a file called pom.xml in the root folder of your project. It contains all the dependencies.
Add this code block inside of the <dependencies> element:
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
This will add the missing dependency to your project. Alternatively, right click on pom.xml and select Maven -> Add Dependency ... and then type commons-logging in the search field. The editor will add the dependency in the right place when you click OK.

How do I manage a 3rd party jar dependency with Eclipse, Maven and Jenkins?

We're trying to manage a project made of multiple Eclipse plug-ins.
One plug-in has a dependency to a 3rd party plug-in. It imports a class from a library named bpmn2. This library is a jar file and NOT included in the Maven remote repository.
Locally, in Eclipse, we have the library checked out into the workspace and referenced in the classpath of the Eclipse project. The plug-in manifest doesn't explicitly state the dependency to bpmn2. Which works locally...
We try to use Jenkins for continuous integration. The Multi-Plug-in-Project is managed using Maven and multiple POM files, using the Maven tycho plug-in.
The problem is that Maven doesn't care for the locally present library bpmn2 (of course). So we thought that using Maven install:install to install bpmn2 to the local Maven repository
./mvn install:install-file -Dfile=/home/someUser/bpmn2/org.eclipse.bpmn2_0.7.0.201111021300.jar -DgroupId=org.eclipse.bpmn2 -DartifactId=bpmn2 -Dversion=0.7.0 -Dpackaging=jar -DlocalRepositoryPath=/var/lib/jenkins/localRep/
and adapting the corresponding POM with a dependency entry for the library
<dependencies>
<dependency>
<groupId>org.eclipse.bpmn2</groupId>
<artifactId>bpmn2</artifactId>
<version>0.7.0</version>
<type>jar</type>
</dependency>
</dependencies>
would work. But it didn't.
The output of Maven is:
[ERROR] Cannot resolve project dependencies:
[ERROR] Software being installed: modeltype.bpmn2 1.0.0.qualifier
[ERROR] Missing requirement: modeltype.bpmn2 1.0.0.qualifier requires 'bundle org.eclipse.bpmn2 0.7.0' but it could not be found
[ERROR]
The question is:
How could we better integrate the 3rd party library?
The local classpath reference is not the optimal solution for the greater picture, I think. Should everyone who develops for the project install the bpmn2 library and only use the dependency in the manifest?
And what are we doing wrong with Maven? The local repository is
/var/lib/jenkins/localRep
and after installing the library to the repository, it seemed that the created dir structure was okay.
/org/eclipse/bpmn2/bpmn2/0.7.0/bpmn2-0.7.0.jar
Can somebody help?
The simplest solution would be to set up a repository server for your company and proxy all your calls through that server.
I use Nexus from Sonatype.
It acts as a proxy when you need to download artifacts from remote locations, like maven central, but it also has an ability to setup repositories to store non-publcicly distributed artifacts.
The whole setup process is very well documented here -> http://www.sonatype.com/books/nexus-book/reference/.
Note, that open-source edition of the product is very good and is enough for your purposes.
Just to be fair, there is a competing product called Artifactory. You may read about it here -> http://www.jfrog.com/products.php
I had a similar scenario where I need to include a third party library (not loaded in maven repository) in Maven Web project.
Following approach helped us make the code portable.
Solution:
Created the "lib" directory under WEB_INF as below:
<<Project_Base_Dir>>/src/main/webapp/WEB-INF/lib
Copied the third party JARs to LIB directory.
Updated POM.xml to use the SYSTEMPATH as below:
<dependency>
<groupId>GROUP_ID</groupId>
<artifactId>ARTIFACT_ID</artifactId>
<version>VERSION</version>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/<<JAR_FILENAME>></systemPath>
<scope>system</scope>
</dependency>
If you can't use Nexus, you can use scope system :
Dependency Scopes
system: This dependency is required in some phase of your
project's lifecycle, but is
system-specific. Use of this scope
is discouraged: This is considered an
"advanced" kind of feature and should
only be used when you truly understand
all the ramifications of its use,
which can be extremely hard if not
actually impossible to quantify.
This scope by definition renders your
build non-portable. It may be
necessary in certain edge cases. The
system scope includes the
<systemPath> element which points to
the physical location of this
dependency on the local machine. It is
thus used to refer to some artifact
expected to be present on the given
local machine an not in a repository;
and whose path may vary
machine-to-machine. The systemPath
element can refer to environment
variables in its path: ${JAVA_HOME}
for instance.
You can use it with
Be careful, using this is probably making your build not portable.