1

I'm building an Automation Tool using Java Automation Framework under the hood, to which users will not have access to the main POM.xml file. Sometimes users require to add a custom Java function which requires additional depedencies / repositories. Presently I have to make changes to the main POM file to accommodate the user request. User has access only to "src/test/java/com/script/custom" folder to write custom scripts / functions. I have explored options like Parent/Child POM, Plugin Management, Profile, etc. but examples are mainly for multiple projects. I'm a NodeJs/Angular person, so I'm a beginner at Java.

Project
  |
  |--src/test/java/com/script/custom
  |                             |
  |                             custom_code.java
  |                             |
  |                             custom_pom.xml
  |
  --pom.xml 

Users should only enter additional dependencies / repos in custom_pom.xml. Parent pom.xml will still hold the main dependencies/repos of the project.

Renish
  • 165
  • 6
  • Does `custom_code` test Java code in `Project/src/main/java` or something else? – Gerold Broser Jun 21 '21 at 20:36
  • custom_code.java is inside "src/test/java/com/script/custom" – Renish Jun 22 '21 at 04:03
  • This is against one of Maven's core concepts: code in `src/test` tests code in `src/main`. See also [Standard Directory Layout](https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html): "_The `src` directory contains all of the source material for building the project, its site and so on. It contains a subdirectory for each type: [...] `test` for the unit test code and resources_". See [my answer](https://stackoverflow.com/a/68074333/1744774). – Gerold Broser Jun 22 '21 at 09:40

1 Answers1

0

Running code (apart from tests) is against the core concept of Maven as a build tool. There are ways, however, to excute arbitrary code at build time:

  • Exec Maven Plugin
  • without an additional plugin (and with cleanly separated projects):
  +- project
  |  +- pom.xml
  +- custom
     +- src/main/java/com/script/custom
     |  +- CustomCode.java  ... convention for Java class names is CamelCase
     +- src/test/java/com/script/custom
     |  +- CustomCodeTest.java  ... instantiates and runs CustomCode 
     +- pom.xml  ... containing <parent><relativePath>../project

For <parent> see Introduction to the POM #Project Inheritance. See also Maven: Lifecycle vs. Phase vs. Plugin vs. Goal for further basics.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107