2

I'm using eclipse with maven. After running my configuration, I always do the same tasks (clean the project, then debug as java app). Is there a way I could add these 2 tasks to the maven configuration ?

Thanks

John156
  • 83
  • 1
  • 1
  • 6
  • Why do you clean the project with Maven? Eclipse rebuilds it automatically when a file changes. No clean should be necessary. – Aaron Digulla Aug 09 '11 at 15:55
  • After I run my maven config, which includes clean, install and android:deploy, if I debug as 'android application without cleaning the project first, I get this error: Failed to install *.apk on device '*': Local path doesn't exist. I would like to find a way to run this maven config + clean the project + debug as... all in one step, whether using a OS trick, eclipse trick, maven trick, or anything that could solve this issue. – John156 Aug 16 '11 at 12:22
  • This is really odd; deploy should do the install and install should create the file, so I can't see why "clean" would be necessary... See my edits to my answer below how to do this with less clicks. – Aaron Digulla Aug 16 '11 at 12:35
  • Yeah I don't know either why cleaning the project is necessary, all I know is that it removes the "Local path doesn't exist" error. Maybe it's just a mistake in my configuration, as it apparently doesn't locate the apk properly. Any idea about this issue would be welcomed too, although this is far less significant since I found a work-around. – John156 Aug 16 '11 at 13:06
  • Just use `Alt+F5` combination. Eclipse automatically cleans and builds your project. It also additionally updates any of the maven dependencies. – TheLuminor Sep 28 '15 at 10:09

3 Answers3

1

Maven is great if your work flow fits into the standard.

If that's not the case, your best bet is to add a build.xml Ant build file to the project which you can use to write "macros" to run repetitive tasks.

Use the exec task to invoke Maven from Ant.

If you can achieve everything with Maven command line options, another approach is to install the m2eclipse plugin. That allows you to create "launch configurations" which run Maven with the options you want.

The tab "Common" allows you to save the launch config as a file in your project. You can also add it as a favorite to the Run or Debug menu. That way, it always stays in the same spot in the menu which makes it more simple to hit with the mouse or a key combination.

[EDIT] You can't start the debugger from Maven or Ant but you can do the other three steps. Create a Maven launch config (Run... -> Run Configurations ... -> Select Maven -> New (+)") and put clean install android:deploy in the field Goals.

That runs all three in sequence. See my other comments above to make this more comfortable.

[EDIT2] You need a macro extension for that. See this question: Is there a Macro Recorder for Eclipse?

Also consider to use two target directories, one for Eclipse and one for Maven. If you run mvn clean, this always confuses Eclipse which doesn't refresh the files in bin or target since no one is supposed to write there but Eclipse itself. See How to configure Maven project to use separate output folders in Eclipse.

Copied the important bits here to avoid broken links:

<project>
  ...

  <build>
    <outputDirectory>${basedir}/${target.dir}/classes</outputDirectory>
    <testOutputDirectory>${basedir}/${target.dir}/test-classes</testOutputDirectory>
  </build>

  <properties>
    <target.dir>target</target.dir>
  </properties>

  <profiles>
    <profile>
      <id>eclipse-folders</id>
      <properties>
        <target.dir>target-eclipse</target.dir>
      </properties>
    </profile>
  </profiles>
  ...  
Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Sorry for being misunderstood, but this had already been done. What I would like now would be to do two normal eclipse tasks after this maven task: a 'clean project' and a 'debug as android application'. And thus do these 3 tasks (running the maven config, which is 'clean install android:deploy', cleaning the project, and 'debug as android application') with only 1 shortcut. – John156 Aug 16 '11 at 13:00
  • Don't use the same target folder for Eclipse and Maven; that should fix your issue. See my 2nd edit. – Aaron Digulla Aug 17 '11 at 08:56
  • Thanks, having two target directories solves the 'clean' issue. I still need a way to do 2 tasks in one step though, one being running a maven config (clean, install, android:deploy), the other being running 'debug as... android application. Is there no other solution than macros for that ? And if not, then how to do it with a macro editor, such as 'practically macro' ? – John156 Aug 17 '11 at 12:19
  • FYI. This link appears invalid: http://m2eclipse.sonatype.org/m2eclipse-faq.html#8 – D-Klotz Oct 12 '16 at 14:44
0

I don't know of any support for Maven for cleaning the Eclipse project or debug.

Eclipse provides some Ant tasks that allow refreshing the workspace, or executing a "clean" build (see http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/ant_eclipse_tasks.htm). These Ant tasks may be reused with the AntRunner, but it would be hard, as they are Eclipse-specific, and depend on some Eclipse plug-ins.

Sadly, I don't know of neither Ant task nor Maven option to execute a Run configuration (that is required for debugging)... Simply executing the Java app should be possible, but I'm not sure, that should be part of the build - it is really a different task in my opinion.

Zoltán Ujhelyi
  • 13,788
  • 2
  • 32
  • 37
  • I know it's not the same task, but still, I could create a shortcut so that all these 3 operations are executed one after the other, for 99% of the time when I compile my app it's to run it in debug mode. If maven doesn't provide that, then maybe it's possible to set it using eclipse tools ? – John156 Aug 09 '11 at 13:15
  • Sadly, I don't know of any such way except coding a plug-in in Java - but that could mean a serious investment of time, as you have to understand the Resources API (file system handling) and Launch configurations (for executing programs - this API is quite hard to understand). But I am sure it is possible to do what you want this way. – Zoltán Ujhelyi Aug 09 '11 at 15:27
0

This seems to work, not the most optimal solution, You need m2eclipse plugin for this

First step is add maven-ant-run plugin to your pom.xml, If you look at the plugin config, It is set up to execute this plugin after compile phase

<plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <property name="runtime_classpath" refid="maven.runtime.classpath"/>
                    <java classname="main class name">
                        <classpath>
                            <pathelement path="${runtime_classpath}"/>
                        </classpath>
                    </java>
                </target>
            </configuration>
        </execution>
    </executions>
  </plugin>
</plugins>

From eclipse debug menu select Debug Configuration

Maven Build ==> New ==> add clean compile in Goal (click Browse Workspace select workspace)

If yo click Debug, It will bring debug perspective and also takes you first break point in your app. It is not finding the source though, but if you click on edit source look up button and just select workspace it seems be able to let you see the source as u debug.

Prasanna Talakanti
  • 2,354
  • 1
  • 16
  • 16
  • I do have the m2eclipse installed too. I tried to do what you said, but it did not work as expected. Maybe it's because it's not just a java application, but an android one ? My maven configuration consists itself of clean, install and android:deploy, and then I'd like to do a java clean of the project, followed by a debug as android application. I would like all these 3 operations (maven config + clean + debug as) to be done in one step, but clean compile doesn't launch the application (not even in no-debug mode). – John156 Aug 10 '11 at 13:20
  • As long as you can run this app in debug mode using eclipse, i don't think where you deploy the app should matter. I am wondering if maven-ant-run is being called, You can test this running compile plugin from m2eclipse, add this echo command right after target tag :, if ant-run is running, you should see this out put in std out [echo] Ant run plugin is being executed. – Prasanna Talakanti Aug 10 '11 at 14:02
  • Hello again, sorry for the delay I had a more urgent issue to solve first. So I tried , and it does display a message. What exactly am I supposed to change from the snippet above ? In particular, what to insert in , since I don't have a public static void main(String []), this is android application. Besides, where can I add that I want a 'clean' and 'debug as android app' when the maven task is finished ? – John156 Aug 16 '11 at 12:08