├── bin └── .gitignore ├── CONTRIBUTING.md ├── src └── at │ └── oebb │ └── playground │ ├── .gitignore │ └── Cleanup.groovy ├── .gitignore ├── README.md └── CODE_OF_CONDUCT.md /bin/.gitignore: -------------------------------------------------------------------------------- 1 | /at/ 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Feel free 2 | -------------------------------------------------------------------------------- /src/at/oebb/playground/.gitignore: -------------------------------------------------------------------------------- 1 | /Script.groovy 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /lib/ 2 | /.settings/ 3 | /.classpath 4 | /.project 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nexus3-cleanup-release-artifact 2 | Nexus Repository Manager 3 supports groovy scripts which can be uploaded and run in the Nexus Manager. 3 | 4 | This groovy script will clean up your nexus repository by deleting outdated components. 5 | 6 | You can define the 7 | 8 | - retentionDays 9 | - retentionCount 10 | - repositoryName 11 | - whitelist 12 | 13 | at the beginning of the script and then run it to clean up your nexus repository. 14 | 15 | For more details on how to run the groovy scripts on Nexus 3 follow this link: https://support.sonatype.com/hc/en-us/articles/115015812727-Nexus-3-Groovy-Script-development-environment-setup 16 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at magamet@hotmail.de. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /src/at/oebb/playground/Cleanup.groovy: -------------------------------------------------------------------------------- 1 | import org.sonatype.nexus.repository.storage.StorageFacet; 2 | import org.sonatype.nexus.common.app.GlobalComponentLookupHelper 3 | import org.sonatype.nexus.repository.maintenance.MaintenanceService 4 | import org.sonatype.nexus.repository.storage.ComponentMaintenance 5 | import org.sonatype.nexus.repository.storage.Query; 6 | import org.sonatype.nexus.script.plugin.RepositoryApi 7 | import org.sonatype.nexus.script.plugin.internal.provisioning.RepositoryApiImpl 8 | import com.google.common.collect.ImmutableList 9 | import org.joda.time.DateTime; 10 | import org.slf4j.Logger 11 | 12 | // ---------------------------------------------------- 13 | // delete these rows when this script is added to nexus 14 | RepositoryApiImpl repository = null; 15 | Logger log = null; 16 | GlobalComponentLookupHelper container = null; 17 | // ---------------------------------------------------- 18 | 19 | def retentionDays = 30; 20 | def retentionCount = 10; 21 | def repositoryName = 'maven-releases'; 22 | def whitelist = ["org.javaee7.sample/javaee7-simple-sample", "org.javaee7.next/javaee7-another-sample"].toArray(); 23 | 24 | 25 | log.info(":::Cleanup script started!"); 26 | MaintenanceService service = container.lookup("org.sonatype.nexus.repository.maintenance.MaintenanceService"); 27 | def repo = repository.repositoryManager.get(repositoryName); 28 | def tx = repo.facet(StorageFacet.class).txSupplier().get(); 29 | def components = null; 30 | try { 31 | tx.begin(); 32 | components = tx.browseComponents(tx.findBucket(repo)); 33 | }catch(Exception e){ 34 | log.info("Error: "+e); 35 | }finally{ 36 | if(tx!=null) 37 | tx.close(); 38 | } 39 | 40 | if(components != null) { 41 | def retentionDate = DateTime.now().minusDays(retentionDays).dayOfMonth().roundFloorCopy(); 42 | int deletedComponentCount = 0; 43 | int compCount = 0; 44 | def listOfComponents = ImmutableList.copyOf(components); 45 | def previousComp = listOfComponents.head().group() + listOfComponents.head().name(); 46 | listOfComponents.reverseEach{comp -> 47 | log.info("Processing Component - group: ${comp.group()}, ${comp.name()}, version: ${comp.version()}"); 48 | if(!whitelist.contains(comp.group()+"/"+comp.name())){ 49 | log.info("previous: ${previousComp}"); 50 | if(previousComp.equals(comp.group() + comp.name())) { 51 | compCount++; 52 | log.info("ComCount: ${compCount}, ReteCount: ${retentionCount}"); 53 | if (compCount > retentionCount) { 54 | log.info("CompDate: ${comp.lastUpdated()} RetDate: ${retentionDate}"); 55 | if(comp.lastUpdated().isBefore(retentionDate)) { 56 | log.info("compDate after retentionDate: ${comp.lastUpdated()} isAfter ${retentionDate}"); 57 | log.info("deleting ${comp.group()}, ${comp.name()}, version: ${comp.version()}"); 58 | 59 | // ------------------------------------------------ 60 | // uncomment to delete components and their assets 61 | // service.deleteComponent(repo, comp); 62 | // ------------------------------------------------ 63 | 64 | log.info("component deleted"); 65 | deletedComponentCount++; 66 | } 67 | } 68 | } else { 69 | compCount = 1; 70 | previousComp = comp.group() + comp.name(); 71 | } 72 | }else{ 73 | log.info("Component skipped: ${comp.group()} ${comp.name()}"); 74 | } 75 | } 76 | 77 | log.info("Deleted Component count: ${deletedComponentCount}"); 78 | } 79 | --------------------------------------------------------------------------------