How to set up Maven in an Android Project under Eclipse - java

Basically I have been fiddling around to get an existing android project to work with maven under eclipse luna.
So here is the way the project was set up to begin with.
The project is a simple android project, which references two external .jars (those are in the libs folder and referenced via add external jars) and then also three additional selfmade libraries which are simple android project but set up to be libraries (via the Is Library checkbox in the Android project properties).
So like this:
Workspace
MainProject -> Android Project
libs
lib1.jar -> referenced in the build path via the add external jars
lib2.jar -> referenced in the build path via the add external jars
SelfMadeLib1 -> Android Project -> set up to be a library and is also referenced in the MainProject via the Add which is available in the Android part of the project properties
SelfMadeLib2 -> Android Project -> set up to be a library and is also referenced in the MainProject via the Add which is available in the Android part of the project properties
SelfMadeLib3 -> Android Project -> set up to be a library and is also referenced in the MainProject via the Add which is available in the Android part of the project properties
Well this was the way I always did it, but since there is maven I figured I could get around this and use maven. So I went a head and removed the self made libs, created a new workspace specifically for them.
Then I added the projects via Import... -> Existing Android Code into Workspace. After that I simply used the Maven Android plugin to convert them via Configure -> Convert To Maven Project. After fixing the referenced Java SDK (set it up to use the old 1.6_45 on my system) I could create the projects via maven and install them in my repo, so no problem there.
Next thing was to configure the existing MainProject, but this did not work. I was able to convert the project into a maven project as well (same way as above with the selfmade libs) and also add the selfmade libs via maven. They are also shown in the Maven Dependencies and I also checked that those dependencies should be included in the project (via Order and Export tab in the Java Build Path of the project properties)
However when I try to debug the whole thing it does not work because of a ClassNotFoundException which gets thrown as soon as I start the app.
So next thing I did was simply to reused the original android project and add the libs the old way (via add external jars) and this works ofc. After analysing the project I simply figured that the maven project does not have the libs because they are not shown under Android Private Libraries in the Java Build Path of the project properties.
So my question is, was anybody able to get android project to work with maven and if so how ?
EDIT:
as requested here is the pom file as well
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyanid.android.dota2.soundboard</groupId>
<artifactId>xyanid.android.dota2.soundboard</artifactId>
<version>1.3.0.0</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>de.xyanid.android.database</groupId>
<artifactId>xyanid.android.database</artifactId>
<version>1.0.0.0</version>
</dependency>
<dependency>
<groupId>de.xyanid.android.gui</groupId>
<artifactId>xyanid.android.gui</artifactId>
<version>1.0.0.0</version>
</dependency>
</dependencies>
</project>
note that de.xyanid.android.database als contains ormlite.core, ormlite-android as well as another selfmade lib. I also tried to include thos as well but that did not change anything.

Related

Easily add libraries to Eclipse Java Oxygen

I know there are post about JAVA libraries in Eclipse and I used a way to add some .jar files (like apache.poi and jsoup) to my project. My question is:
Where do we have to put those libraries and what settings do we have to change, so to make the libraries available for every future project we start ?
Thank you!
The most common tools for bringing dependencies into a Java project are Maven, Gradle, and Ant. I'll focus on Maven, as I estimate it to be the most popular of the three (though I have come to prefer Gradle).
I'm going to assume you have the following prerequisites installed on your machine and each of the developers on your team:
JDK 1.7 or above (in other words, Java)
A Java-focused IDE like IntelliJ or Eclipse
Here's what you need to do:
You and your team members will install Maven
You will create a pom.xml for each new project.
You will specify the dependencies for the project (like jsoup) in your pom.xml
Here's what Maven will do for you:
It will download all the dependencies you specify from an online repository like https://mvnrepository.com/
It will cache the dependencies in a directory in your user home folder called .m2 so that it doesn't need to download dependencies more than once per project
It will resolve the dependencies within each of your dependencies and avoid putting the same dependency on the classpath twice
The minimum pom.xml file you need is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourcompany</groupId>
<artifactId>your-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
</dependencies>
</project>
Copy that into a file called pom.xml in your root project directory. You can add any additional dependencies in the <dependencies> section of the XML file. By convention, all of your classes and code should go into the directory structure src/main/java/.
Any Java IDE you use will know how to open a project from a pom.xml file. In IntelliJ, you select File->New->Project from existing sources, navigate to your project's root directory, select your pom.xml, and click Open. IntelliJ will then open your project as a Maven project, read your pom.xml, and make all of your dependencies indexed and available for code completion and compilation.
Hope this helps

Using third-party libraries in Eclipse RCP Tycho app

I've created a boiler-plate project following vogella's extensive Tycho tutorial.
Facts:
There's no feature, and there's no plugin. The only plugin is the RCP app, which is also the entry-point.
Problem:
I have no idea in which pom.xml do I include the 3rd party dependencies.
I cannot include them in the RCP project, because the packaging of that pom is eclipse-plugin, and not jar. From what I've noticed, if I change the packaging to jar, then the "Maven Dependencies" library is added automatically. If I change back to eclipse-plugin, they get removed.
Questions:
Where do I add the dependencies? There's no pom with jar packaging in my project.
Should I create a separate project with the necessary JARs? How do I include that dependency to my entire project?
Is it really that much of a good practice to create a separate plugin and a feature for this RCP app?
Related solutions:
"Update projects" doesn't work, and neither do the n other solutions in the other SO questions.
There's also this question and that question, but I don't fully get the answers
I think that you have a fundamental misunderstanding.
Maven: Maven determines all of the project dependencies via the pom.xml and resolves transitive dependencies automatically (assuming that all of the pom files and artifacts exist in repositories that you've configured and correctly declare their dependencies).
Tycho: The problem is that Eclipse already has its own project model based on product files, feature.xml files, and plug-in MANIFEST.MF files. Tycho leverages the Maven machinery for Eclipse, but the idea is that the pom.xml files just configure the Maven plug-ins and declare the packaging type. That provides an entry point for Maven, but then Tycho takes over. While Maven would normally build the dependency chain from information in the pom.xml files, Tycho is building the dependency change from information in the product, feature, and MANIFEST.MF files. You don't put any dependencies in the pom.xml files. Tycho also uses Eclipse p2 repositories (instead of normal Maven repositories) for finding dependent plug-ins that are not found in the local modules or target platform.
That's actually a benefit for many Eclipse developers since they've already set up everything properly in their Eclipse plug-ins, features, and products. They do not want to have to repeat all of the dependencies in the pom.xml.
Using Libraries in Eclipse plug-ins: In Eclipse, if you want to use a library that is not already packaged as an Eclipse plug-in, you have a few options. Your plug-in can include a set of JARs in a libs folder and then include that libs folder in the plug-in and runtime classpath (see the build.properties file). Another option is to create your own "library plug-in" that repackages a JAR library as an Eclipse plug-in. See also https://wiki.eclipse.org/FAQ_What_is_the_classpath_of_a_plug-in%3F. That's the answer that you're getting above.
The problem is that if you're trying to include a complex library with multiple JARs that is normally distributed and included in a standard Java project via Maven. We hit this problem with the Jersey JAX-RS implementation in my project. There's no p2 repository that includes all of the pieces of the libraries as plug-ins with correct dependency information.
Easy Solution: If you need a common library, check the Orbit project first to see whether the libraries have already been packaged as Eclipse plug-ins, http://www.eclipse.org/orbit/. In that case, you can download them and include them in your target platform, or you can pull them in dynamically at (Tycho) build time from their p2 repository. Your plug-ins would just include those plug-ins as dependencies (in the their MANIFEST.MF files).
Workaround / Solution: In our case, Jersey JAX-RS was not available as an Eclipse plug-in, and it had a bunch of transitive dependencies. The workaround was to create an Eclipse "library plug-in" like I mentioned above with two pom files. We initially created a skeleton plug-in with an empty libs folder. One pom file is just a standard Maven pom file with <packaging>jar</packaging> that declares the top-level dependencies required to pull in the Jersey JAX-RS implementation and all of its dependencies. The dependencies are declared with <scope>compile</scope>. We use the maven-dependency-plugin to copy all of those dependencies into the project's libs folder.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
We actually ended up running Maven with that pom by hand from time to time to update the libs, and then we just checked the plug-in with all of its dependent JARs into source control. Checking the build later, I see that we actually populate the libs folder on-the-fly with Maven with a separate build task just before we start the Maven/Tycho part of the build. Of course, plug-in's MANIFEST-MF file's Bundle-ClassPath and Export-Package entries are coming straight from source control. We have to check those from time to time to ensure that they match the libraries and packages that we're getting from Maven. (That doesn't tend to change much unless we bump major library versions or add a new dependency at the Maven level.) The plug-in's build.properties has the libs/ folder as part of bin.includes.
In the development environment, after we first check out the code, we just run mvn (with an External Tools launch config that's also checked in with the project) on the project's "copy dependencies" pom file. That populates the libs folder with all of the JAX-RS libraries and dependencies. We only have to run it again when we update something about the dependencies or when we're jumping between branches that have different versions of the JAX-RS dependencies. We set .gitignore to ensure that we don't commit the libs to Git.
The other pom for this project is set up like a normal Tycho pom file with <packaging>eclipse-plugin</packaging>. During our automated build, we run one step early in the build process (just after check out) that calls mvn with the jar pom to populate the libs. Then we proceed with the main Maven/Tycho build using the eclipse-plugin pom. The eclipse-plugin pom has no dependency information (as I said above). It's just providing Tycho a way to recognize the Eclipse plug-in and build it based on its MANIFEST.MF and build.properties files. But the built plug-in includes and exposes all of those libs that were populated by the mvn call to the jar pom step.
So, it's a bit of a mess, but that's the best solution we found a couple of years ago when we hit this problem. I'm not sure whether Tycho is doing any work to permit some sort of hybrid Maven/Tycho build that could do this automatically as part of the build. I guess I should ask the developers. :)
Your questions:
Where do I add the dependencies? There's no pom with jar packaging in my project. Answer: The workaround above lets you do it with one project. You just have two pom files, like pom_deps.xml and pom.xml. You just have to invoke the pom_deps.xml separately to populate the libs folder (in the dev environment and with your automated builds).
Should I create a separate project with the necessary JARs? How do I include that dependency to my entire project? Answer: the workaround that I described above lets you do it with a single project. Another way to do it is to create a separate JAR project, but I don't think that your Eclipse RCP app can really include a <packaging>jar</packaging> module in a useful way. The only way I've found to do it is to use a similar workaround. You build the JAR module first, install it into the maven repository, and then have one of your plug-in projects bundle the JAR in its libs folder. (If you really want to do it that way, ask. We have a case where we have to do that, too, and I can provide the steps we do in development and the build to make it work. I think the single project workaround that I provided above makes more sense for your case.)
Is it really that much of a good practice to create a separate plugin and a feature for this RCP app? Answer: that's really a separate question. If you have a feature with multiple plug-ins, you have the same problem. Tycho can handle the product/feature/plug-ins, but it cannot jump across into Maven-based dependency resolution. You'll end up having to use the same workarounds
Summary: The fundamental issue is that Eclipse plug-ins can't "see" a bare JAR library. The plug-in needs to have the library included in its local libs folder (with a matching Bundle-ClassPath entry in MANIFEST.MF), or it needs to depend on some other plug-in that exports the appropriate packages. Tycho just resolves dependencies via Eclipse plug-ins, and it cannot leverage normal Maven dependency resolution directly to pull in a bunch of JARs. If all of your dependencies are already plug-ins, you're fine. If not, you may have to use the workaround above to package a set of libraries for your plug-ins to use.
Just adding the plugin to pom dependencies and including the entry <pomDependencies>consider</pomDependencies> in the configuration of target-platform-configuration makes it work.
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho.version}</version>
<configuration>
<!-- The configuration to make tycho consider the maven dependencies -->
<pomDependencies>consider</pomDependencies>
<!-- other configurations -->
</configuartion>
</plugin>
<!-- other plugins-->
</plugins>
<dependencies>
<!-- An example third-party bundle (plugin) present in maven repository-->
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.gogo.shell</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Reference link here.

Create Apklib for building with maven

I have Android project A which refers Android library project B, Building Project A on eclipse works fine. But my app using some system API so I thought to build using maven. I am not getting how to include Project B in Project A on maven.
(basically proj B has both source and resource)
Basically I generated B.jar and edited pom file
<dependency>
<groupId>com.me.example</groupId>
<artifactId>myId</artifactId>
<version>2.4.1</version>
<type>lib</type>
</dependency>
then I am able to build A.apk but when I launch A.apk application, its crashing. Because project B has some resources and when I generate B.jar it has only class files and don't have resources. And also I tried to generate B.apk and included included in maven and I couldn't able to build A.apk.
How can I solve this.
First I built B.apklib then I included it in A project as dependency and now I am able to build A.apk
B.pom contains
<groupId>com.mer</groupId>
<artifactId>me</artifactId>
<version>1.4.3</version>
<packaging>apklib</packaging>
<name>I_am</name>
<dependencies>
.......
</dependencies>

Maven Multimodule project issue?

Hi I have multimodule maven project something like this..
parent
Core
Web
and my Web project depend on Core project classes so i added Core project as a dependency in Web project pom.xml file.
But from inside eclipse when i am running Web project the lib directory does not contain Core-project.jar file in class-path so project not running. How can resolve this issue?..Plugin I used in my Web Projec *Pom.xml* file..
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
</plugin>
</plugins>
And i am using Tomcat6 Server.
this Dependency tag in my Web Project pom.xml File...
<dependency>
<groupId>org.csdc</groupId>
<artifactId>core-java</artifactId>
<version>5.0.0</version>
</dependency>
And When i run web project from inside Eclipse i am not getting core.jar in this path....
workspace_maven.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\web\WEB-INF\lib
Anyone got any such issue .If yes please let me know how can i resolve this issue?
Under Eclipse : you do not need to have your core.jar in your classpath. Check your librairies (web->properties->java build path->librairies), you should see your core folder and not your core.jar.
Make sure that "resolve workspace dependencies" is checked in your maven build target.
Install the m2e and m2e-wtp plugins for eclipse. m2e-wtp handles web projects and tomcat. You don't need the plugins sections of your pom that you've documented in the question.
For maven to work with projects in eclipse you need to ensure that the parent project is at the same level as the child projects and the pom of the parent project is like below
<modules>
<module>../project1</module>
<module>../project2</module>
</modules>
Unfortunately this is the only way to get maven to work correctly in eclipse and jenkins for multi-module builds.
Not exactly clear that this is what you are looking for, but the jars won't appear under your lib directory in eclipse because they are in your repository.
My version of eclipse has a 'maven' menu when I right click on the project. If yours does too then make sure that you have 'Enable Maven Nature' selected. This will make a small M appear next to the project name and a new folder in the project, which contains all of the dependencies listed in your pom.
1.Go to web project build path -> Libraries - Add variable -> Configure Variables -> New -> Enter Name as M2_REPO and Path as C:\Users\usernmee.m2\repository and click Ok.
All the dependencies will configured in the project build path. So no compilation error in web projecct.
2.Maven project need to take war and deploy it in any web container(tomcat,etc).
Check the war in your target directory after running mvn clean package. If it is missing there, check the scope of your dependency for "Core" in your pom.xml for "Web", make sure that it is not provided or test.
It sounds like from your pom fragment that you are using mvn eclipse:eclipse (which you shouldn't be anymore), instead use the m2eclipse plugin: http://www.sonatype.org/m2eclipse.
Install the correct set of plugins and configure them correctly.
If you need a more detailed answer, you should give us more information what you tried to achieve, which plugins you use, what kind of web server, how you deploy, whether you use Maben to deploy or the Eclipse WTP, etc.
[EDIT] You won't see anything in project/WEB-INF/lib when you start the project in Eclipse - there is no point putting anything in there because this folder isn't used.
Instead, you probably deploy the project to a web server. There are plugins for Eclipse which can do that (i.e. start a web server, deploy your project into it) but since you don't mention how exactly you "start" the project inside of Eclipse, which version of Eclipse you're using and which buttons you click, how you configured Eclipse, etc. it's really hard to help you.

How to reference javadocs to dependencies in Maven's eclipse plugin when javadoc not attached to dependency

I use Eclipse, Maven, and Java in my development. I use Maven to download dependencies (jar files and javadoc when available) and Maven's eclipse plug-in to generate the .project and .classpath files for Eclipse. When the dependency downloaded does not have attached javadoc I manually add a link for the javadoc in the .classpath file so that I can see the javadoc for the dependency in Eclipse. Then when I run Maven's eclipse plugin to regenerate the .classpath file it of course wipes out that change.
Is there a way to configure Maven's eclipse plug-in to automatically add classpath attributes for javadoc when running Maven's eclipse plug-in?
I'm only interested in answers where the javadoc and/or sources are not provided for the dependency in the maven repository, which is the case most often for me. Using downloadSources and/or downloadJavadocs properties won't help this problem.
From the Maven Eclipse Plugin FAQ
The following example shows how to do
this in the command-line:
mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true
or in your pom.xml:
<project>
[...]
<build>
[...]
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
[...]
</plugins>
[...]
</build>
[...]
</project>
I'm running STS 2.8.1 which is basically eclipse + spring tools; In an existing maven project, I right clicked on the project -> maven -> Download Sources and Download JavaDocs
As mentioned in How to download sources and javadoc artifacts with Maven Eclipse plugin from other repository?, you can do this:
In Eclipse go to Windows-> Preferences-> Maven. Check the box that says "Download Artifact Javadoc." That has worked well for me.
You might consider just avoiding this problem completely by installing the javadoc jar into your local repository manually using the install-file goal and passing in the -Dclassifier=javadoc option. Once you do that the .classpath that mvn generates should be correct.
If you use a remote repo as a proxy to central you could also deploy the javadocs to that repo and then everyone else who uses that proxy will now get the javadocs automatically as well.
Generally Javadocs are not primarily used as dependency . Because these are neither required at compile nor runtime. It’s just to help the developer while developing or debugging.
Assuming using the java IDE Eclipse we can use the java docs as referenced. Following are the approaches we can associate the javadocs/sources with the respective jars.
1. If it’s non-maven project :
Download the javadocs jar or zipped file, whatever available and placed it in some directory.
Right click on the application project in the IDE Eclipse, click Properties and choose Java Build Path then select tab Libraries under the Java Build Path. Now expand the jar you want to link with java docs/source. Select the Javadoc location link and click on Edit button, a new window appears where we need to choose the javadocs jar path. Click OK and we have linked the javadoc/source with the respective jars.
2. If it’s a maven project
If we are using the Maven project then go to jar files under the Maven dependency under the project in Project Explorer view as shown below. Now right click on the jar file you want to add the Javadoc/source,  choose Maven then click on Javadoc or Source you want to link with the project. Now IDE will automatically download the required javadoc/source and will link it with the respective jar in the project.
You can verify this by right click on the project in the IDE and click on Java Build Path and select the Libraries tab under the Java Build Path and then expand the desired jar, here when you click the Edit button you will see the linked path of the Javadoc/Source with the respective jar as shown below in the image.
3. If it’s Maven project and we are setting the default behavior:
Eclipse will aquatically download the javadoc/source along with the main required jar at the starting.
By default setting instruction to Maven to download the Javadoc/sources for all the jars linked in the project.
Click Windows – preferences – select Maven and click the checkbox Download Artifact Javadoc as shown below
Now click on apply and save it and now when you create new Maven project , by default the Javadocs will get downloaded and linked with all the dependent jars in the project.
You can verify by right click on the project and Properties and under Java Build path can see the javadocs are linked with all the jars as shown below.
If your project is Maven project then It’s always best to use 2nd approach because by using this approach the IDE and Maven, takes care of downloading the correct version of the Javadoc/source and linked it with the relative jar as well.
Approach 3rd is bit costly because the javadoc/sources will be downloaded for-all the dependent jars, may be you are not interested for javadocs/sources for all the dependent jars.
Would having the sources for the dependency help? You can tell the eclipse plugin to download those (and refer to them in the .classpath) with -DdownloadSources=true

Categories

Resources