3

I have a pom like this. all I am trying to do is install the file as llews-1.0-test.jar instead of llews-1.0.jar. while this pom install a jar as llews-1.0-test.jar, it also install the llews-1.0.jar(default) one. how do I disable the default install?

<build>
            <finalName>llews-${project.version}-test</finalName>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.3.1</version>
                    <executions>                        
                        <execution>
                            <id>install test</id>
                            <phase>install</phase>
                            <goals>
                                <goal>install-file</goal>
                            </goals>

                            <configuration>
                                <file>${project.build.directory}\${project.build.finalName}.jar</file>
                                <pomFile>${basedir}\pom.xml</pomFile>
                                <classifier>test</classifier>
                            </configuration>
                        </execution>
                    </executions>                   
                </plugin>
</build>
Nan
  • 199
  • 1
  • 12
  • My question would be why do you want to do that? I cant think of a reasonable use case.. – Manfred Moser Mar 16 '12 at 20:51
  • because I am building a test jar used in the test envirnment, I use this name so people won't use by wrong – Nan Mar 16 '12 at 21:41
  • 1
    so just name the artifactId with a test in it rather than using classifier.. that would be the better, cleaner approach and people using it do not have to use classifier when they depend on it. You are overloading classifier with something it is not meant for and therefore are making things more difficult than necessary. – Manfred Moser Mar 16 '12 at 21:51
  • 1
    So what _is_ classifier intended for then? https://maven.apache.org/guides/mini/guide-building-for-different-environments.html – Ed Randall Oct 02 '16 at 08:57
  • @Manfred Moser Reasonable use case might also be the following pragmatic answer https://stackoverflow.com/a/47733421 – Filou Aug 03 '23 at 13:57

2 Answers2

3

The best way may be to replace the default jar plugin execution, like this

  <plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
      <execution>
        <id>default-jar</id>
        <configuration>
          <classifier>test</classifier>
        </configuration>
      </execution>
    </executions>
  </plugin>

This way your default artifact will just have the required classifier and both install and deploy will do the right thing by default.

Of course you should not additionally modify finalName then, as the jar plugin will add the classifier to it.

Note: while this answers the question as given, I agree with Manfred Moser that what you want to do does not look like a good idea.

Michał Politowski
  • 4,288
  • 3
  • 30
  • 41
  • basically, in this project, I have 2 profiles, 1 for test and 1 for production. when I use the test profile, I want to install llews-1.0-test.jar. production profile for production for llews-1.0-prod.jar, but not find a good way to do this. – Nan Mar 20 '12 at 20:30
  • @Nan - If your artifacts are really similar enough that you feel profiles are a better fit than separate modules, and if you really want to install both variants into a repository then classifiers are probably the way to go. – Michał Politowski Apr 01 '12 at 21:56
  • In my case, I want to build a regular library jar and make it available to other projects in the usual way. However, I _also_ want to publish the runnable version of the jar. I can build either using build targets (`install` for regular, `spring-boot:repackage` to create runnable jar). How to `deploy` each one? Creating an entire project for single "main" class, seems silly ... – Charlie Reitzel Apr 05 '21 at 22:21
  • If someone is using `maven-ejb-plugin` (most probably also `maven-war-plugin`; not tested) id of the execution needs to be `default-ejb` (`default-war` for maven-war-plugin). – Filou Aug 03 '23 at 13:54
  • In maven 3.6 this worked fine. In maven 3.9.4 I get the following error: `The packaging plugin for this project did not assign a main file to the project but it has attachments. Change packaging to 'pom'.` – Christian Aug 23 '23 at 14:50
  • Interesting. Change caused by https://issues.apache.org/jira/browse/MDEPLOY-205 – Michał Politowski Aug 23 '23 at 20:01
0

You should not use classifier for signalling it is a test artifact. Instead of all your finalName hack and the jar plugin reconfiguration just change the artifactId to leews-test.

That way it is clear that it is a test artifact from the name and a dependency to it can use it without needing a classifier too and everything will work out of the box.

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
  • 1
    but in this case, when I use another profile to build production jar, how do i change this name in the profile? – Nan Mar 20 '12 at 20:18
  • oh... well.. it is not a test artifact in the sense that it contains test but in the sense that it is configured for the QA/test environment? If that is the case you could use the approach you do .. or use a separate module for each one. – Manfred Moser Mar 20 '12 at 21:22
  • right. let me clarify this a little. we have test environment and production environment. different jar for different setup. to avoid misuse, I'd like to have different name for them. so I use classifier to differentiate them. but what should I do if I want to build a jar with all test case? still classifier? – Nan Mar 20 '12 at 21:35